可以看到很多的APP在第一次運行之后就會彈出來一個Toast說什么快捷方式已創建,那么這個東西是怎么搞出來的呢
很簡單就下面幾句話,寫在這兒以后好copy
先創建一個類
1 import android.app.Activity; 2 import android.content.Intent; 3 import android.os.Parcelable; 4 5 /** 6 * Created by Administrator on 2016/1/21. 7 */ 8 public class ShortCut { 9 public static void createShortCut(Activity act, int iconResId, int appnameResId) { 10 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 11 // 不允許重復創建 12 intent.putExtra("duplicate", false); 13 // 需要現實的名稱 14 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,act.getString(appnameResId)); 15 // 快捷圖片 16 Parcelable icon = Intent.ShortcutIconResource.fromContext(act.getApplicationContext(), iconResId); 17 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 18 // 點擊快捷圖片,運行程序 19 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(act.getApplicationContext(), act.getClass())); 20 // 發送廣播 21 act.sendBroadcast(intent); 22 23 } 24 }
上面的代碼
首先生成一個Intent實例(隱式Intent),然后向intent中放入一些值來配置
上面的就是放入了4個值來控制快捷圖標的創建
1.是否允許重復創建
2.確定創建的圖標的名稱
3.確定創建的圖標的圖片
4.設置點擊這個快捷圖標就運行該APP
你可以在你的APP第一次運行時在第一個Activity中調用這個類的靜態方法,就實現了快捷圖標的創建
最后不要忘了添加權限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>