這是關於launcher的開發,目的是launcher 自動給新安裝的app創建快捷方式 , 不是 app給自己創建快捷方式到workspace。求指點,謝謝!
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, info.title.toString());
Intent respondIntent = info.intent;
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);
Context pkgContext = null;
int iconIdentifier = 0;
PackageManager manager = mLauncher.getPackageManager();
System.out.println("manager="+manager);
android.content.pm.ApplicationInfo appInfo;
try {
appInfo = manager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
iconIdentifier = appInfo.icon;
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
try {
pkgContext = mLauncher.createPackageContext(packageName,Context.CONTEXT_IGNORE_SECURITY
| Context.CONTEXT_INCLUDE_CODE);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
if (pkgContext != null) {
ShortcutIconResource iconRes = Intent.ShortcutIconResource
.fromContext(pkgContext, iconIdentifier);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconRes);
}
mLauncher.sendBroadcast(shortcut);