get Random Short String - Android java.util

Android examples for java.util:Random String

Description

get Random Short String

Demo Code


import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class Main{
    private static ArrayList<String> mNameList;
    public static String getRandomShortString() {
        return getRandomName();
    }/* w  w w . j  a  v a 2s.  co m*/
    public static String getRandomName() {
        if (mNameList == null) {
            PackageManager pm = Test.sApp.getPackageManager();
            List<ApplicationInfo> appList = pm
                    .getInstalledApplications(PackageManager.GET_META_DATA);
            mNameList = new ArrayList<>(appList.size());
            for (ApplicationInfo ai : appList) {
                mNameList.add(ai.loadLabel(pm).toString());
            }
        }
        if (mNameList == null || mNameList.size() == 0) {
            return "Default Name";
        }
        int idx = random(mNameList.size());
        return mNameList.get(idx);
    }
    public static int random(int n) {
        return (new Random()).nextInt(n);
    }
}

Related Tutorials