本文實例講述了Android編程實現創建,刪除,判斷快捷方式的方法。分享給大家供大家參考,具體如下:
/*** 為程序創建桌面快捷方式 ,這樣寫,在程序卸載的時候,快捷方式也會一并刪除*/private void addShortcut() { Intent shortcutIntent = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 快捷方式的名稱 shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); shortcutIntent.putExtra("duplicate", false); // 不允許重復創建 /* * shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent( * getApplicationContext(), SplashActivity.class)); */ // 注意: ComponentName的第二個參數必須加上點號(.),否則快捷方式無法啟動相應程序 ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName()); Intent intent = new Intent(Intent.ACTION_MAIN); intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent.setComponent(comp)); // 快捷方式的圖標 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext( this, R.drawable.icon_launcher); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); sendBroadcast(shortcutIntent);}//判斷是否已經創建快捷方式private boolean hasShortcut() { boolean isInstallShortcut = false; final ContentResolver resolver = this.getContentResolver(); final String AUTHORITY; if (android.os.Build.VERSION.SDK_INT < 8) { AUTHORITY = "com.android.launcher.settings"; } else { AUTHORITY = "com.android.launcher2.settings"; } final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = resolver .query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { this.getString(R.string.app_name).trim() }, null); if (c != null && c.getCount() > 0) { isInstallShortcut = true; } return isInstallShortcut;}更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答