Android編程添加快捷方式(Short)到手機(jī)桌面的方法(含添加,刪除及查詢)
本文實(shí)例講述了Android編程添加快捷方式(Short)到手機(jī)桌面的方法。分享給大家供大家參考,具體如下:
權(quán)限
要在手機(jī)桌面上添加快捷方式,首先需要在manifest中添加權(quán)限。
<!-- 添加快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <!-- 移除快捷方式 --> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <!-- 查詢快捷方式 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
添加快捷方式
添加快捷方式,是向桌面應(yīng)用(launcher)發(fā)送相關(guān)action的廣播,相關(guān)的action如下:
添加快捷方式:
private void addShortcut(String name) {
Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);
// 不允許重復(fù)創(chuàng)建
addShortcutIntent.putExtra("duplicate", false);// 經(jīng)測(cè)試不是根據(jù)快捷方式的名字判斷重復(fù)的
// 應(yīng)該是根據(jù)快鏈的Intent來判斷是否重復(fù)的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名稱不同時(shí),雖然有的手機(jī)系統(tǒng)會(huì)顯示Toast提示重復(fù),仍然會(huì)建立快鏈
// 屏幕上沒有空間時(shí)會(huì)提示
// 注意:重復(fù)創(chuàng)建的行為MIUI和三星手機(jī)上不太一樣,小米上似乎不能重復(fù)創(chuàng)建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 圖標(biāo)
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(MainActivity.this,
R.drawable.ic_launcher));
// 設(shè)置關(guān)聯(lián)程序
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.setClass(MainActivity.this, MainActivity.class);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 發(fā)送廣播
sendBroadcast(addShortcutIntent);
}
移除快捷方式
移除快捷方式的action:
移除快捷方式的方法:
private void removeShortcut(String name) {
// remove shortcut的方法在小米系統(tǒng)上不管用,在三星上可以移除
Intent intent = new Intent(ACTION_REMOVE_SHORTCUT);
// 名字
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
// 設(shè)置關(guān)聯(lián)程序
Intent launcherIntent = new Intent(MainActivity.this,
MainActivity.class).setAction(Intent.ACTION_MAIN);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 發(fā)送廣播
sendBroadcast(intent);
}
在兩個(gè)手機(jī)上測(cè)試,發(fā)現(xiàn)小米手機(jī)上添加了快捷方式后不能移除,三星手機(jī)可以。
查詢快捷方式
查詢快捷方式是否存在的方法是從網(wǎng)上其他資料那里查來的,但是測(cè)試查詢的時(shí)候失敗了,兩個(gè)手機(jī)(小米、三星)都查不到。
先留著代碼以后看看是什么原因吧:
private boolean hasInstallShortcut(String name) {
boolean hasInstall = false;
final String AUTHORITY = "com.android.launcher2.settings";
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
// 這里總是failed to find provider info
// com.android.launcher2.settings和com.android.launcher.settings都不行
Cursor cursor = this.getContentResolver().query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { name }, null);
if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
}
return hasInstall;
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android應(yīng)用創(chuàng)建桌面快捷方式代碼
- Android如何創(chuàng)建桌面快捷方式
- Android程序開發(fā)之手機(jī)APP創(chuàng)建桌面快捷方式
- Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
- 解析Android應(yīng)用啟動(dòng)后自動(dòng)創(chuàng)建桌面快捷方式的實(shí)現(xiàn)方法
- Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
- android 為應(yīng)用程序創(chuàng)建桌面快捷方式技巧分享
- Android編程實(shí)現(xiàn)向桌面添加快捷方式的方法
- Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
- Android應(yīng)用創(chuàng)建多個(gè)快捷方式
- Android實(shí)現(xiàn)向Launcher添加快捷方式的方法
- Android編程創(chuàng)建桌面快捷方式的常用方法小結(jié)【2種方法】
相關(guān)文章
android 獲取本機(jī)其他app的版本信息的示例代碼
本篇文章主要介紹了android 獲取本機(jī)其他app的版本信息的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
在Android中實(shí)現(xiàn)浮窗并添加吸邊效果的代碼示例
在 Android 中實(shí)現(xiàn)浮窗(懸浮窗)并添加吸邊效果,可以使用 WindowManager 來管理浮窗視圖,并通過觸摸事件來實(shí)現(xiàn)吸邊效果,以下是一個(gè)示例,展示如何創(chuàng)建一個(gè)浮窗并實(shí)現(xiàn)吸邊效果,需要的朋友可以參考下2025-02-02
Android okhttputils現(xiàn)在進(jìn)度顯示實(shí)例代碼
本文通過實(shí)例代碼給大家詳細(xì)介紹了Android okhttputils現(xiàn)在進(jìn)度顯示,代碼簡(jiǎn)答易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
Android webview如何加載HTML,CSS等語言的示例
本篇文章主要介紹了Android webview如何加載HTML,CSS等語言的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Android監(jiān)聽ScrollView滑動(dòng)距離的簡(jiǎn)單處理
這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽ScrollView滑動(dòng)距離的簡(jiǎn)單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android 抽屜效果的導(dǎo)航菜單實(shí)現(xiàn)代碼實(shí)例
本篇文章主要介紹了Android 抽屜效果的導(dǎo)航菜單實(shí)現(xiàn)代碼實(shí)例,這種側(cè)滑的抽屜效果的菜單很好,有興趣的可以了解一下。2016-12-12
Flutter中使用setState時(shí)的6個(gè)簡(jiǎn)單技巧總結(jié)
平常在使用flutter的控件時(shí)我們都知道,要刷新頁面那么只需要調(diào)用setState()方法即可,這篇文章主要給大家介紹了關(guān)于Flutter中使用setState時(shí)的6個(gè)簡(jiǎn)單技巧,需要的朋友可以參考下2022-05-05
解決Eclipse創(chuàng)建android項(xiàng)目無法正常預(yù)覽布局文件問題的方法
這篇文章主要介紹了解決Eclipse創(chuàng)建android項(xiàng)目無法正常預(yù)覽布局文件問題的方法,需要的朋友可以參考下2015-12-12

