0

I have an activity that can retrieve all applications that just like Launcher AllAppsList (or I should call them activities precisely, since an application can have multiple activities, like Google map). And this activity can broadcast an Intent to create shortcuts for any of these application. And my problem is about the shortcut created. Here is my code create shortcut:

public static void addShortcut(Context c, ActivityInfo actInfo){
    PackageManager pm = c.getPackageManager() ;
    String packageName = actInfo.packageName ;
    String className = actInfo.name ;
    CharSequence label = actInfo.loadLabel(pm) ;

    // Create an Intent to launch the application
    Intent shortcutIntent = new Intent (Intent.ACTION_MAIN) ;
    shortcutIntent.setComponent(new ComponentName(packageName, className)) ;
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
    // Create an Intent to notify Launcher to create a shortcut on home screen
    Intent addIntent = new Intent() ;
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent) ;
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label) ;
    // Put in the shortcut's icon
    Log.i(TAG, "use actInfo.loadIcon(pm)") ;
    Drawable drawable = actInfo.loadIcon(pm) ;
    Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, b) ;

    // Broadcast the Intent
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    c.sendBroadcast(addIntent) ;
}

This can successfully create a shortcut on home screen, but those icons' size are almost not correct. They could be too small or too large. Like this image.. The upper row is 2 shortcuts I created. The 2 shortcuts below are dragged from Launcher's AllAppsView and they are normal. Apparently mine are smaller...

The first thing I've tried is resizing the icons. But if I do so, the icon created would not be as perfect as those I drag from Launcher's AllAppsView. In other words, they became vague.

Another try is this code:

 if (actInfo.icon != 0){
        Log.i(TAG, "use actInfo.icon") ;
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(c, actInfo.icon)) ;
    }
    else if (actInfo.applicationInfo.icon != 0){
        Log.i(TAG, "use actInfo.applicationInfo.icon") ;
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(c, actInfo.applicationInfo.icon)) ;
    }

which is from here, but this cannot work either.

Wish somebody can give me a hand:)

Community
  • 1
  • 1
Joy Wen
  • 46
  • 4

1 Answers1

0

what i think you should do is :

final int appIconSize=(int)(getResources().getDisplayMetrics().density*48);
final Bitmap temp;
if(shortcutIconBitmap.getWidth()==appIconSize&&shortcutIconBitmap.getHeight()==appIconSize)
    temp=shortcutIconBitmap;
else temp=Bitmap.createScaledBitmap(shortcutIconBitmap,appIconSize,appIconSize,true);
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON,temp);

if you have the app run on API 11 and above only, you can use getLauncherLargeIconSize instead, but i haven't used it much, so i can only say that for galaxy s3, both methods return the same value (96).

android developer
  • 114,585
  • 152
  • 739
  • 1,270