zoukankan      html  css  js  c++  java
  • 【Android进阶】快捷图标的创建与移除

    注释已经说得很清楚了,如果有疑问,请留言

    /**
    	 * 添加桌面快捷方式
    	 * 
    	 * @param view
    	 */
    	public void click1(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, "LOL");
    		// 设置图标
    		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);
    	}
    

    /**
    	 * 删除桌面快捷方式
    	 * 
    	 * @param view
    	 */
    	public void click2(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, "LOL");
    		// 设置图标
    		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);
    	}

    /**
    	 * 判断桌面是否已经存在快捷方式
    	 * 
    	 * @return
    	 */
    	private boolean isExit() {
    		Uri uri = null;
    		// 不同版本中的Uri不同
    		if (getSdkVersion() < 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[] { "LOL" };
    		Cursor cursor = getContentResolver().query(uri, null, selection,
    				selectionArgs, null);
    		if (cursor.moveToNext()) {
    			cursor.close();
    			return true;
    		} else {
    			cursor.close();
    			return false;
    		}
    	}
    

    /**
    	 * 获得手机SDK的版本
    	 * 
    	 * @return
    	 */
    	private int getSdkVersion() {
    		return android.os.Build.VERSION.SDK_INT;
    	}

    需要声明的权限

        <!-- 添加快捷方式权限 -->
        <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"/>

    点击下载源代码

  • 相关阅读:
    【NOIP2006T】作业调度方案
    「NOIP2012P」寻宝
    51Nod1084 矩阵取数问题 V2
    51Nod1294 修改数组
    51nod1122 机器人走方格V4
    多项式相关模板#1
    51Nod1514 美妙的序列
    Jzoj5674 香槟
    Jzoj5669 排列
    Jzoj5665 奥立卡的诗
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3749569.html
Copyright © 2011-2022 走看看