1.在清單文件中添加權(quán)限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2.在清單文件的activity節(jié)點(diǎn)添加意圖過濾器(點(diǎn)擊快捷方式打開的activity)
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="com.jaychan.demo.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
其中的action節(jié)點(diǎn)中的name屬性自己定義,一般都是app的包名然后加點(diǎn)東西就行了
3.代碼
//創(chuàng)建快捷方式
private void installShortcut() {
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "我的app");// 快解方式名稱
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory
.decodeResource(getResources(), R.mipmap.app_icon));// 快解方式圖標(biāo)
Intent actionIntent = new Intent();
actionIntent.setAction("com.jaychan.demo.MAIN"); //需要和清單文件定義的那個(gè)action一致
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, actionIntent);
sendBroadcast(intent);
}