日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 创建,验证和删除桌面快捷方式 (删除快捷方式测试可用)

發布時間:2023/12/2 Android 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 创建,验证和删除桌面快捷方式 (删除快捷方式测试可用) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

測試環境為Adnroid 2.1以上。

第一步:AndroidManifest.xml 權限配置:

添加快捷方式權限:

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

驗證快捷方式是否存在權限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

刪除快捷方式權限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

代碼:

1 public class ShortCutSample { 2 /** 3 * 添加快捷方式 4 * */ 5 public void creatShortCut(Activity activity,String shortcutName,int resourceId) 6 { 7 Intent intent = new Intent(); 8 intent.setClass(activity, activity.getClass()); 9 /*以下兩句是為了在卸載應用的時候同時刪除桌面快捷方式*/ 10 intent.setAction("android.intent.action.MAIN"); 11 intent.addCategory("android.intent.category.LAUNCHER"); 12 13 Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 14 15 //不允許重復創建 16 shortcutintent.putExtra("duplicate", false); 17 //需要現實的名稱 18 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName); 19 //快捷圖片 20 Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId); 21 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 22 //點擊快捷圖片,運行的程序主入口 23 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); 24 //發送廣播。OK 25 activity.sendBroadcast(shortcutintent); 26 } 27 /** 28 * 刪除快捷方式 29 * */ 30 public void deleteShortCut(Activity activity,String shortcutName) 31 { 32 Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); 33 //快捷方式的名稱 34 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName); 35 //在網上看到到的基本都是一下幾句,測試的時候發現并不能刪除快捷方式。 36 //String appClass = activity.getPackageName()+"."+ activity.getLocalClassName(); 37 //ComponentName comp = new ComponentName( activity.getPackageName(), appClass); 38 //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 39 /**改成以下方式能夠成功刪除,估計是刪除和創建需要對應才能找到快捷方式并成功刪除**/ 40 Intent intent = new Intent(); 41 intent.setClass(activity, activity.getClass()); 42 intent.setAction("android.intent.action.MAIN"); 43 intent.addCategory("android.intent.category.LAUNCHER"); 44 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent); 45 activity.sendBroadcast(shortcut); 46 } 47 /** 48 * 判斷是否存在快捷方式 49 * */ 50 public boolean hasShortcut(Activity activity,String shortcutName) 51 { 52 String url = ""; 53 int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK); 54 /*大于8的時候在com.android.launcher2.settings 里查詢(未測試)*/ 55 if(systemversion < 8){ 56 url = "content://com.android.launcher.settings/favorites?notify=true"; 57 }else{ 58 url = "content://com.android.launcher2.settings/favorites?notify=true"; 59 } 60 ContentResolver resolver = activity.getContentResolver(); 61 Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null); 62 if (cursor != null && cursor.moveToFirst()) { 63 cursor.close(); 64 return true; 65 } 66 return false; 67 } 68 }

調用測試代碼:

 public class mainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ShortCutSample sample =new ShortCutSample();String shortcutName=getString(R.string.app_name);if(sample.hasShortcut(this, shortcutName))sample.deleteShortCut(this,shortcutName);elsesample.creatShortCut(this,shortcutName,R.drawable.icon);} }

在網上找了很久都是一樣的代碼,刪除那塊搞了一個下午才弄好,其實很簡單的東東。

第一次發文章,Adnroid新人。多多交流和指導呀。呵呵。

轉載于:https://www.cnblogs.com/yeqw1985/archive/2013/02/06/2907704.html

總結

以上是生活随笔為你收集整理的Android 创建,验证和删除桌面快捷方式 (删除快捷方式测试可用)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。