Android 创建、删除ShortCut

1.添加权限 

在Android4.4中使用了新的launcher,路径名有点变化。

<!-- Android4.4版本的启动器的permission -->
<uses-permission android:name="com.google.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.google.android.launcher.permission.WRITE_SETTINGS" />
<!-- 4.4非原生启动器的permission -->
<uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
<!-- Android4.4以下版本的启动器的permission -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
<!-- 创建快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<!-- 删除快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
2.判断是否存在shortcut

public boolean hasShortcut(Activity activity) {
	String url = "";
<span style="white-space:pre">	</span>String authority;
<span style="white-space:pre">	</span>authority = getAuthority(context);
<span style="white-space:pre">	</span>if (TextUtils.isEmpty(authority)) {
<span style="white-space:pre">		</span>return true;// 没有查询到,就不添加了
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>url = "content://" + authority + "/favorites?notify=true";
<span style="white-space:pre">	</span>ContentResolver resolver = context.getContentResolver();
<span style="white-space:pre">	</span>Cursor cursor = resolver.query(Uri.parse(url), new String[] { "title","iconResource" }, "title=?", new String[] { title }, null);
<span style="white-space:pre">	</span>if (cursor != null && cursor.moveToFirst()) {
<span style="white-space:pre">		</span>cursor.close();
<span style="white-space:pre">		</span>return true;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return false;
}
3.根据permission得到authority,来得到完整的查询数据库的字符串

public static String getAuthority(Context context) {
<span style="white-space:pre">	</span>List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
<span style="white-space:pre">	</span>if (packs != null) {
<span style="white-space:pre">		</span>for (PackageInfo pack : packs) {
<span style="white-space:pre">			</span>ProviderInfo[] providers = pack.providers;
<span style="white-space:pre">			</span>if (providers != null) {
<span style="white-space:pre">				</span>for (ProviderInfo provider : providers) {
<span style="white-space:pre">					</span>if (!TextUtils.isEmpty(provider.readPermission)
<span style="white-space:pre">							</span>&& provider.readPermission.contains("launcher"))
<span style="white-space:pre">						</span>return provider.authority;
<span style="white-space:pre">					</span>if (!TextUtils.isEmpty(provider.writePermission)
<span style="white-space:pre">							</span>&& provider.writePermission.contains("launcher"))
<span style="white-space:pre">						</span>return provider.authority;
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return null;
}
4.创建shortcut

/**
 * 创建快捷方式
 * 
 * @param context
 * @param intent
 *            返回应用的shortcut:intent.addCategory(Intent.CATEGORY_LAUNCHER);
 *                               intent.setAction(Intent.ACTION_MAIN);
 *            进入其他的shortcut:intent.setAction
 *            (指定一个,在启动的activity中的IntentFilter-Action也需要加上)
 */
public static void createShortcut(Context context, Intent intent) {
<span style="white-space:pre">	</span>ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, intent.getIntExtra("resId", 0));
<span style="white-space:pre">	</span>Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
<span style="white-space:pre">	</span>shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,intent.getStringExtra("title"));
<span style="white-space:pre">	</span>shortcutIntent.putExtra("duplicate", false);
<span style="white-space:pre">	</span>shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
<span style="white-space:pre">	</span>shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
<span style="white-space:pre">	</span>context.sendBroadcast(shortcutIntent);
}

5.根据Intent删除快捷方式

/**
 * 删除程序创建的快捷方式
 * 
 * @param context
 * @param intent
 *            需要和创建时传入的Intent相同
 */
public static void deleteShortcut(Context context, Intent intent) {
	Intent shortcutIntent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
	shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,intent.getStringExtra("title"));
	shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
	context.sendBroadcast(shortcutIntent);
}
6.根据名称删除快捷方式

/**
 * 根据名称删除shortcut
 * 
 * @param context
 * @param title
 */
public static void deleteShortcut(Context context, String title) {
	final ContentResolver cr = context.getContentResolver();
	Cursor c = cr.query(Uri.parse("content://" + getAuthority(context)
					+ "/favorites?notify=true"), new String[] { "_id",
					"title", "iconPackage" }, "title=?",
			new String[] { title }, null);// 根据title查询出来
	while (c.moveToNext()) {
		cr.delete(Uri.parse("content://" + getAuthority(context)+ "/favorites/" + c.getLong(0) + "?notify=false"),null, null);// 根据_id删除
	}
	c.close();
	cr.notifyChange(Uri.parse("content://" + getAuthority(context)+ "/favorites?notify=true"), null);// 刷新
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值