直接在手機桌面上建立應用的快捷方式

使用如下代碼,即可以在桌面上建立一個應用的快捷方式:

		Intent shortcutIntent = new Intent(ACTION_INSTALL_SHORTCUT);  
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
		shortcutIntent.putExtra(EXTRA_SHORTCUT_DUPLICATE, false);  
		Intent intent = new Intent();  
		intent.setComponent(new ComponentName(this.getPackageName(), ".MainActivity"));  
		  
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
		                        Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));  
		sendBroadcast(shortcutIntent);   

需要添加權限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章