记录google标准响应桌面创建APP快捷方式的方法。
1、manifest中声明创建快捷方式创建:
<activity
android:name="XXXX.ShortCutActivity"
<!--Activity无页面,防止某些机器上会闪一下-->
android:theme="@android:style/Theme.NoDisplay"
<!--允许外部调用-->
android:exported="true" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<!--接收action-->
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>2、添加快捷方式::
/**
* 添加指定包名的快捷方式
*
* @param context
* @param packageName
* @return
*/
public static boolean addShortcut(Context context, String packageName) {
try {
Context pkgContext = context;
if (!AstApp.self().getPackageName().equals(packageName)) {
//避免某些机器上出现安装问题
pkgContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
}
PackageManager pm = pkgContext.getPackageManager();
ApplicationInfo info = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(pkgContext, info.icon);
String name = info.loadLabel(pm).toString().trim();
Intent launcheIntent = pm.getLaunchIntentForPackage(packageName);
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
s
// 不允许重复创建
shortcut.putExtra("duplicate", false);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcheIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
context.sendBroadcast(shortcut);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return true;
}注意事项:
1、要有:
android:theme="@android:style/Theme.NoDisplay"
否则某些机器上创建时候,会闪一下,这是因为打开了一个activity,因此,必须指明其无页面显示。
2、在oncreate中创建,创建完成后立刻调用finish()
3、避免重复创建,添加
shortcut.putExtra("duplicate", false);
4、相关交互方式属于google标准,稍微关注即可
本文详细记录了如何在Google应用中通过manifest文件声明创建桌面快捷方式的活动,包括设置无显示主题、接收特定意图动作以及添加快捷方式的具体步骤。确保避免重复创建并遵循Google标准交互方式。
2367

被折叠的 条评论
为什么被折叠?



