在Android Jellybean上为其他应用程序创建快捷方式,但大小错误

时间:2013-01-25 09:20:59

标签: android icons size shortcut

我有activity可以检索与Launcher AllAppsList一样的所有应用程序(或者我应该准确地将它们称为活动,因为应用程序可以有多个activities,例如Google地图) 。此activity可以广播Intent以创建任何这些应用程序的快捷方式。我的问题是关于创建的快捷方式。这是我的代码创建快捷方式:

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) ;
}

这可以在主屏幕上成功创建快捷方式,但这些图标的大小几乎不正确。它们可能太小或太大。 Like this image..上排是我创建的2个快捷方式。下面的2个快捷方式是从Launcher的AllAppsView拖出的,它们是正常的。显然我的小... ...

我尝试过的第一件事就是调整图标大小。但是,如果我这样做,创建的图标将不如我从Launcher的AllAppsView拖动那样完美。换句话说,它们变得含糊不清。

另一个尝试是这段代码:

 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,但这也行不通。

希望有人能帮助我。)

1 个答案:

答案 0 :(得分:0)

我认为你应该做的是:

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);

如果您的应用程序仅在API 11及更高版本上运行,您可以使用getLauncherLargeIconSize代替,但我没有使用它,所以我只能说对于galaxy s3,两种方法都返回相同价值(96)。