开发者

Intent to pick an installed application slow in my activity, why?

开发者 https://www.devze.com 2022-12-15 08:01 出处:网络
If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications.I needed this same functionality in my applica

If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications. I needed this same functionality in my application so I copied the intent from the launcher source:

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);            
        Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
        pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
        this.startActivityForRe开发者_C百科sult(pickIntent, MoreIconsConstants.REQUEST_PICK_APPLICATION)

When this executes on the launcher it is pretty speedy. When I execute this in my activity it takes maybe 15 seconds instead of 3. It seems like the launcher must be caching this data for some amount of time? Is there any way I can cache the data too?

Thanks!


You can read all the apps that install by this code

final PackageManager pm = a.getPackageManager();

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    final List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
    Collections.sort(apps, new ResolveInfo.DisplayNameComparator(pm));

    for (int i = 0; i < apps.size(); i++) 
    {
        ResolveInfo info = apps.get(i);
       //Intent to start the app
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,info.activityInfo.name));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        //Load app icon
        info.activityInfo.loadIcon(pm)
        //Load app label
        info.loadLabel(pm)
}

Check this example code http://developer.android.com/resources/samples/Home/src/com/example/android/home/Home.html search for loadApplications function

I had the same problem so what I did is to load the application by the code above in a thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消