Android編程創(chuàng)建桌面快捷方式的常用方法小結(jié)【2種方法】
本文實(shí)例講述了Android編程創(chuàng)建桌面快捷方式的常用方法。分享給大家供大家參考,具體如下:
Android在桌面上生成快捷方式有兩種情況,一種是直接在桌面直接生成;一種是長(zhǎng)按桌面,在彈出的快捷菜單中生成。
談?wù)勗谧烂嫔现苯由?。個(gè)人覺(jué)得這個(gè)比較爽快,既然都是快捷方式了干嘛還要再隱藏一層呢?當(dāng)然喜歡桌面干凈的就比較喜歡第二個(gè)了。
第一個(gè)是通過(guò)廣播(Broadcast)的形式向Luncher發(fā)送請(qǐng)求生成快捷方式的。
在網(wǎng)上找到關(guān)于這方面的注冊(cè)信息。
<!--設(shè)置wallpapaer的activity --> <!-- Intent received used to install shortcuts from other applications --> <receiver android:name="com.android.launcher2.InstallShortcutReceiver" android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> <intent-filter> <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" /> </intent-filter> </receiver>
可以看出,要在桌面上創(chuàng)建快捷方式就需要權(quán)限了:
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT
。
所以在我們的manifest.xml文件中,我們需要加入下面這段話:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
下面就是代碼層的實(shí)現(xiàn):
假如我在一個(gè)activity中創(chuàng)建一個(gè)創(chuàng)建快捷方式的方法:createShortCut();
public void createShortCut(){ //創(chuàng)建快捷方式的Intent Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //不允許重復(fù)創(chuàng)建 shortcutintent.putExtra("duplicate", false); //需要現(xiàn)實(shí)的名稱 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname)); //快捷圖片 Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //點(diǎn)擊快捷圖片,運(yùn)行的程序主入口 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class)); //發(fā)送廣播。OK sendBroadcast(shortcutintent); }
二、長(zhǎng)按桌面彈出的桌面快捷方式創(chuàng)建
第一頁(yè)談過(guò)直接在桌面生成快捷方式,現(xiàn)在說(shuō)說(shuō)如何在添加到一個(gè)SHORTCUTS列表中,就是你長(zhǎng)按桌面彈出來(lái)的那個(gè)東東。
首先在注冊(cè)activity時(shí),需要添加一個(gè)action為android.intent.action.CREATE_SHOERTCUT
的intentFilter.如下所示:
<activity android:name="ShortCutTest"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter> </activity>
接下來(lái)就是就是設(shè)置快捷方式的圖標(biāo)、名稱、事件等屬性。這里圖表的生成,android里提供了專(zhuān)門(mén)的方法來(lái)生成。
public class ShortCutTest extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } public void createShortCut(){ Intent addShortCut; //判斷是否需要添加快捷方式 if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){ addShortCut = new Intent(); //快捷方式的名稱 addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "我的快捷方式"); //顯示的圖片 Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon); addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); //快捷方式激活的activity,需要執(zhí)行的intent,自己定義 addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent()); //OK,生成 setResult(RESULT_OK, addShortCut); }else{ //取消 setResult(RESULT_CANCELED); } } }
PS:關(guān)于AndroidManifest.xml文件相關(guān)屬性功能可參考本站在線工具:
Android Manifest功能與權(quán)限描述大全:
http://tools.jb51.net/table/AndroidManifest
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android RecyclerView網(wǎng)格布局示例解析
這篇文章主要介紹了Android RecyclerView網(wǎng)格布局示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Android 狀態(tài)欄的設(shè)置適配問(wèn)題詳解
這篇文章主要介紹了Android 狀態(tài)欄的設(shè)置適配問(wèn)題詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android開(kāi)發(fā)中Activity的生命周期及加載模式詳解
這篇文章主要介紹了Android開(kāi)發(fā)中Activity的生命周期及加載模式詳解的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android Studio和阿里云數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序
本文主要介紹了Android Studio和阿里云數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11詳解Android權(quán)限管理之RxPermission解決Android 6.0 適配問(wèn)題
本篇文章主要介紹了Android權(quán)限管理之RxPermission解決Android 6.0 適配問(wèn)題,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
這篇文章主要為大家詳細(xì)介紹了Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android基于繪制緩沖實(shí)現(xiàn)煙花效果
這篇文章主要介紹了Android基于繪制緩沖實(shí)現(xiàn)煙花效果,文中通過(guò)代碼示例和圖文結(jié)合介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動(dòng)手嘗試一下2024-03-03kotlin object關(guān)鍵字單例模式實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了kotlin object關(guān)鍵字單例模式實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01