注意:只能啓動主窗體android
通常來講在 Android 中添加快捷方式的有如下兩種:web
在launcher的應用程序列表上,長按某一應用程序圖標建立快捷方式到桌面app
在桌面上長按在彈出框中選擇快捷方式->應用程序->將添加快捷方式的程序this
那麼能不能在應用安裝時自動將應用的快捷入口添加到桌面呢? 本文給你們分享一下相關的經驗?.net
桌面是由launcher來控制的,因此咱們能夠經過下面兩種方式來實現快捷方式的自動建立:orm
經過向launcher發送Broadcast讓launcher建立快捷方式xml
爲應用程序的組件註冊某一個符合特定條件的IntentFilter,而後能夠直接在Launcher的桌面添加啓動該組件的快捷方式。get
第一種方式:string
/**it
* 添加快捷方式到桌面 要點:
* 1.給Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
* 2.給定義爲Intent.EXTRA_SHORTCUT_INENT的Intent設置與安裝時一致的action(必需要有)
* 3.添加權限:com.android.launcher.permission.INSTALL_SHORTCUT
*/
private void addShortcutToDesktop() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不容許重建
shortcut.putExtra("duplicate", false);
// 設置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,this.getString(R.string.app_name));
// 設置圖標
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 設置意圖和快捷方式關聯程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));
// 發送廣播
sendBroadcast(shortcut);
}
當快捷方式建立成功後,launcher將經過toast的方式提示快捷方式建立成功,其中經過
shortCutIntent.putExtra("duplicate", false);設置不能重複建立,若是快捷方式已經建立則提示快捷方式已經建立
注意若是要讓上述代碼能成功運行,咱們還須要設置Uses permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
第二種方式和第一種有些相似,不過咱們不用廣播的方式讓給launcher建立,而是經過註冊IntentFilter,因爲「添加快捷方式」Action是 由Launcher經過startActivity-ForResult這一方法發出的,在Activity啓動後把初始化的快捷方式 Intent返回給Launcher應用程序,設置結果值爲RESULT_OK表示正常返回。
主要代碼以下:
首先在xml中設置IntentFilter
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
複製代碼建立核心代碼:
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
// 不容許重建
shortcut.putExtra("duplicate", false);
// 設置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
this.getString(R.string.app_name));
// 設置圖標
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 設置意圖和快捷方式關聯的程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(this, this.getClass()));
//將結果返回到launcher
setResult(RESULT_OK, intent);
}
在launcher中咱們運行程序就能夠將快捷方式建立在桌面上。
經過上述方式能夠自動將快捷方式建立到桌面上,可是每次運行程序時都會將快捷方式建立到桌面上,下面咱們將經過程序判斷快捷方式是否已經建立到桌面上了,基本思路是:因爲快捷方式launcher管理的,咱們能夠經過查看launcher中是否已經有此快捷方式數據,若是有就不在建立。
主要代碼以下:
/**
* 添加權限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
*
* @return
*/
private boolean hasInstallShortcut() {
boolean hasInstall = false;
final String AUTHORITY = "com.android.launcher.settings";
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor cursor = this.getContentResolver().query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { this.getString(R.string.app_name) }, null);
if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
}
return hasInstall;
}
上述查詢操做,須要具備如下權限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"></uses-permission>
注意經過程序建立的快捷方式不會隨着程序卸載而自動刪除。