Android 为应用创建多个桌面快捷方式,可在卸载时自动删除桌面快捷方式

引言:有时候,我们需要多个app入口的快捷方式,其实很简单。闲话少说,看下面:

为应用创建快捷方式目前有两种方法:

一、权限:

 1. 在AndroidManifest.xml中添加权限:

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

2. 在启动Activity中发送广播:


对于第一个个程序入口MainActivity,在AndroidManifest.xml里设置

 <activity
            android:name=".MainActivity"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:process=":process.main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

对于第二个个程序入口SecondActivity,在 AndroidManifest.xml里设置

注:@style/translucent是在该页面设置的自定义透明窗体,

      SecondActivity作为另一个入口,让用户进入app的另一个页面,访问指定功能。

<activity
            android:name=".SecondActivity"
            android:icon="@drawable/icon1"
            android:label="@string/app_name1"
            android:launchMode="singleInstance"
            android:theme="@style/translucent" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>


二、添加方法:

1.获得快捷桌面的读取权限,如果有快捷方式,就返回true,否则false

public static boolean hasInstallShortcut(Context context) {
		boolean hasInstall = false;

		String AUTHORITY = "com.android.launcher.settings";
		int systemversion = Build.VERSION.SDK_INT;
		Log.i("Build.VERSION.SDK==========>", systemversion + "");

		if (systemversion >= 8) {
			AUTHORITY = "com.android.launcher2.settings";
		}
		Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
				+ "/favorites?notify=true");
		Cursor cursor = context
				.getContentResolver()
				.query(CONTENT_URI,
						new String[] { "title" },
						"title=?",
						new String[] { context.getString(R.string.shortcutname) },
						null);
		if (cursor != null && cursor.getCount() > 0) {
			hasInstall = true;
		}

		return hasInstall;
	}


2.hasInstallShortcut(Context context)如果返回false,表明没有快捷方式,那就执行创建快捷方式操作方法:

    

public void createShortCut(Context context) {
		// ***************************快速打开APP的桌面快捷方式**********************************

		Intent intent = new Intent();
		intent.setClass(context, context.getClass());
		/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
		intent.setAction("android.intent.action.MAIN");
		intent.addCategory("android.intent.category.LAUNCHER");

		// 创建快捷方式的Intent
		Intent shortcutintent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 需要现实的名称
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.shortcutname));
		// 不允许重复创建
		shortcutintent.putExtra("duplicate", false);
		// 快捷图片
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				getApplicationContext(), R.drawable.icon);
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 点击快捷图片,运行的程序主入口
		shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		// 发送广播。OK
		sendBroadcast(shortcutintent);

		// ***************************快速操作的桌面快捷方式**********************************
		Intent intent1 = new Intent();
		intent1.setClass(context, SecondActivity.class);
		/* 以下两句是为了在卸载应用的时候同时删除桌面快捷方式 */
		intent1.setAction("android.intent.action.MAIN");
		// intent1.addCategory("android.intent.category.LAUNCHER");

		Intent shortcutintent1 = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");

		shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.app_name1));
		shortcutintent1.putExtra("duplicate", false);
		Parcelable icon1 = Intent.ShortcutIconResource.fromContext(
				getApplicationContext(), R.drawable.icon1);
		shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon1);

		shortcutintent1.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent1);

		sendBroadcast(shortcutintent1);

	}

3.为了防止重复创建,只在安装后第一次启动时创建,方法如下:

   注:在自定义的Utils工具类里定义SHOW_CREATE_SHORTCUT_ICON常量,在Utils工具类类用该常量存储布尔值,以达到只创建一次快捷方式的目         的。

if (!Utils.getInstance(MainActivity.this).getBoolean(
				Utils.SHOW_CREATE_SHORTCUT_ICON, false)
				&& hasInstallShortcut(MainActivity.this) == false) {
			// 创建桌面快捷方式
			createShortCut(MainActivity.this);
			Utils.getInstance(getApplicationContext()).save(
					Utils.SHOW_CREATE_SHORTCUT_ICON, true);
			// 提示用户桌面快捷方式创建成功
			Toast.makeText(MainActivity.this, "快捷方式创建成功", Toast.LENGTH_SHORT)
					.show();
		}

这样做完以后,就搞定了。卸载时,快捷方式自动删除。

三、自定义删除快捷方式方法:

[java]  view plain copy
  1. /** 
  2.       * 删除快捷方式 
  3.       * */  
  4.      public static void deleteShortCut(Context context)  
  5.      {  
  6.         Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
  7.         //快捷方式的名称    
  8.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));    
  9.         /**删除和创建需要对应才能找到快捷方式并成功删除**/  
  10.         Intent intent = new Intent();   
  11.         intent.setClass(context, context.getClass());    
  12.         intent.setAction("android.intent.action.MAIN");    
  13.         intent.addCategory("android.intent.category.LAUNCHER");    
  14.           
  15.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);    
  16.         context.sendBroadcast(shortcut);            
  17.      }  



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值