1,实现逻辑
-
发送一个广播,创建快捷方法
-
图标和名字手动的传给系统
-
添加权限
-
bug 会无限创建快捷键,sp存储
-
bug当应用已经开启后,在点快捷键进入,重新开启新的程序。 组件调用
2,实现代码
-
权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
/**
* 创建快捷方式 权限<uses-permission
* android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
*/
private void createShortCut() {
boolean isShortCut = PreferenceUtils.getBoolean(
getApplicationContext(), Constants.SHORT_CUT, false);
if (isShortCut) {
// 如果创建过 就不在创建
return;
}
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// 应用名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "小帅哈哈");
// 应用图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory
.decodeResource(getResources(), R.drawable.ic_launcher));
// 点击打开的页面的意图 注意里面打开和快捷方式打开的问题
Intent actionIntent = new Intent();
actionIntent.setAction("android.intent.action.MAIN");
actionIntent.addCategory("android.intent.category.LAUNCHER");
ComponentName splash = new ComponentName(getApplicationContext(),
SplashActivity.class);
// 指定打开页面的组件对象
actionIntent.setComponent(splash);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
sendBroadcast(intent);
PreferenceUtils.putBoolean(getApplicationContext(),
Constants.SHORT_CUT, true);
}
本文介绍了在Android中创建快捷方式的实现逻辑,包括通过发送广播来创建快捷方式,手动传递图标和名称给系统,设置必要的权限,以及解决可能遇到的bug,如无限创建快捷键问题和应用开启状态下的组件调用问题。

241

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



