Android快捷方式的创建与shortcut是否已创建的判断

在Activity触发事件中调用以下方法:

private void creatShortcut(){
		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);		//不允许重复创建
		Intent intent = new Intent(Intent.ACTION_MAIN);
//		intent.setComponent(new ComponentName(MainPageActivity.this.getPackageName(),  
//		        ".activity.StartActivity"));   //此方式会导致4.0以上系统出现“未安装此应用程序”错误,原因不详,知道的同学望告知
		intent.setClass(MainPageActivity.this, StartActivity.class);

		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
				Intent.ShortcutIconResource.fromContext(MainPageActivity.this,
						R.drawable.icon));
		sendBroadcast(shortcutIntent);
	}

接下来在配置文件中加入:

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

/**
	 * 判断快捷方式是否已经创建
	 */
	public static boolean hasShortcut(Context context) {
	    boolean result = false;
	    // 获取当前应用名称
	    String title = null;
	    try {
	        final PackageManager pm = context.getPackageManager();
	        title = pm.getApplicationLabel(
	                pm.getApplicationInfo(context.getPackageName(),
	                        PackageManager.GET_META_DATA)).toString();
	    } catch (Exception e) {
	    }

	    final String uriStr;
	    if (android.os.Build.VERSION.SDK_INT < 8) {
	        uriStr = "content://com.android.launcher.settings/favorites?notify=true";
	    } else {
	        uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
	    }
	    final Uri CONTENT_URI = Uri.parse(uriStr);
	    final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
	            "title=?", new String[] { title }, null);
	    if (c != null && c.getCount() > 0) {
	        result = true;
	    }
	    return result;
	}

以上方法需要以下权限:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值