Android編程實現(xiàn)向桌面添加快捷方式的方法
本文實例講述了Android編程實現(xiàn)向桌面添加快捷方式的方法。分享給大家供大家參考,具體如下:
有時候為了使用方便,需要在桌面上添加快捷方式,下面是兩種添加快捷方式的方法:
方法1:
void setshortCut() {
Intent addShortcut = new Intent();
// 設置快捷方式的名字
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式練習");
// 構(gòu)建快捷方式中專門的圖標
Parcelable icon = Intent.ShortcutIconResource.fromContext(
ShortcutTest.this, R.drawable.icon);
// 添加快捷方式圖標
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 構(gòu)建快捷方式執(zhí)行的Intent
Intent mailto = new Intent(this, ShortcutTest.class);
// 添加快捷方式Intent
addShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mailto);
// 正常
setResult(RESULT_OK, addShortcut);
}
通過以上代碼添加的快捷方式,只能在程序啟動中添加,不能加入到事件中,現(xiàn)在也沒有想明白是什么,也沒有仔細研究一下,希望知道的朋友通知一下!并且這樣添加的時候,不直接添加到桌面中,而是添加到添加快捷方式窗口中,如圖:

方法2:
private void addShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// shortcut.putExtra("duplicate", false); //不允許重復創(chuàng)建
//指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二個參數(shù)必須加上點號(.),否則快捷方式無法啟動相應程序
// ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,ShortcutTest.class));
//快捷方式的圖標
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
其中:
shortcut.putExtra("duplicate", false);
表示的為不允許重復設置
此種代碼,直接添加到桌面上,并且也在上圖所示的地方顯示,但是此需要添加下面的權(quán)限:
PS:更多關(guān)于Android Manifest權(quán)限控制可參考本站在線工具:
Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
相關(guān)文章
Android制作登錄頁面并且記住賬號密碼功能的實現(xiàn)代碼
這篇文章主要介紹了Android制作登錄頁面并且記住賬號密碼功能的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

