通過launcher源碼可以看到創建快捷方式是通過
– <receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
– <intent-filter>
<action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
</intent-filter>
</receiver>
生成快捷方式
我們可以在程序啟動時直接發送廣播創建桌面快捷方式
private void createShortCut(){
Intent intent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//action
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));//快捷方式名字
intent.putExtra("duplicate", false); //是否重復創建快捷方式
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//icon
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , BatteryProfile.class)); //啟動界面
sendBroadcast(intent);//發送廣播
}
我們需要在在mainfest.xml加入以下權限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
摘自 android 開發專欄