Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android_創建和刪除快捷圖標

Android_創建和刪除快捷圖標

編輯:關於Android編程

	/**
	 * 判斷桌面是否已經存在快捷方式
	 */
	private boolean isExit() {
		Uri uri = null;
		if (android.os.Build.VERSION.SDK_INT < 8) {
			uri = Uri.parse("content://com.android.launcher.settings/favorites");
		} else {
			uri = Uri.parse("content://com.android.launcher2.settings/favorites");
		}
		String selection = "title=?";
		String[] selectionArgs = new String[] { "快捷圖標名稱" };
		Cursor cursor = getContentResolver().query(uri, null, selection, selectionArgs, null);
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		} else {
			cursor.close();
			return false;
		}
	}

	public void createShortcut(View view) {
		if (isExit()) {
			Toast.makeText(getApplicationContext(), "快捷方式已經存在", 0).show();
			return;
		}
		Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo);
		Intent intent = new Intent();
		intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷圖標名稱");
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

		Intent intent2 = new Intent();
		// 點擊圖標意圖
		intent2.setAction(Intent.ACTION_MAIN);
		intent2.addCategory(Intent.CATEGORY_LAUNCHER);
		intent2.setComponent(new ComponentName(this, MainActivity.class));

		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
		sendBroadcast(intent);
	}
	public void delShortcut(View view) {
		Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo);
		
		Intent intent = new Intent();
		intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷圖標名稱");
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		
		Intent intent2 = new Intent();
		intent2.setAction(Intent.ACTION_MAIN);
		intent2.addCategory(Intent.CATEGORY_LAUNCHER);
		intent2.setComponent(new ComponentName(this, MainActivity.class));
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
		
		sendBroadcast(intent);
	}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved