Android創建桌面快捷方式就是在桌面這個應用上創建一個快捷方式,桌面應用:launcher2
通過查看launcher2的清單文件:
1 <!-- Intent received used to install shortcuts from other applications --> 2 <receiver 3 android:name="com.android.launcher2.InstallShortcutReceiver" 4 android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> 5 <intent-filter> 6 <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" /> 7 </intent-filter> 8 </receiver>
可以看出來,創建桌面快捷方式,是通過Broadcast。
那么想要創建一個快捷方式的話,就發送這個廣播就可以了。
同時注意,清單文件中寫的很清楚,需要權限:com.android.launcher.permission.INSTALL_SHORTCUT,所以需要在清單文件中添加權限。
同時需要在添加快捷方式的名稱,圖標和該快捷方式干什么事情(一個含有action等信息的intent)
攜帶的信息:使用intent.pueExtra()
名稱:Intent.EXTRA_SHORTCUT_NAME
圖標:Intent.EXTRA-SHORTCUT_ICON
意圖:Intent.EXTRA_SHORTCUT_INTENT
創建桌面快捷方式的代碼:
1 Intent intent = new Intent(); 2 intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 3 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "桌面快捷方式"); 4 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); 5 Intent shortIntent = new Intent(); 6 shortIntent.setAction("android.intent.action.MAIN"); 7 shortIntent.addCategory("android.intent.category.LAUNCHER"); 8 shortIntent.setClassName(getPackageName(), "com.xxx.mobile.activity.WelcomeActivity"); 9 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortIntent); 10 sendBroadcast(intent);
通過以上代碼,就可以創建一個桌面快捷方式了。