我们知道创建快捷方式是通过发送创建快捷方式的广播,然后确定唯一的Activity是通过action加category,然后android的指引都是intent,所以创建广播的代码中必须要有指向该唯一activity的intent,然后发送广播,由系统把这个intent相关信息添加到桌面launcher要读取的用来保存快捷方式的数据表中,(一般是favorite吧,不过有些手机rom很奇怪,本文只针对主流的miui,魅族,纯净android等主流ROM机型)
知道这些后,所以代码可以这样写了:
1、申请权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>//添加快捷方式
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>//删除快捷方式
2、对要使用快捷方式的activity指定唯一识别方法
<activity android:name="com.demo.shortactivity.ShortActivity">
<intent-filter>
<action android:name="android.intent.demo.test"/>
<category android:name="android.intent.category.thisistest"/>
</intent-filter>
</activity>
3、发送广播
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = new Intent();
shortcutIntent.setAction("android.intent.demo.test");//对应到activity的action
shortcutIntent.addCategory("android.intent.category.thisistest");//对应到category
shortcutIntent.setClass(ShortActivity.this, ShortActivity.class);//对应到响应的class
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//放入intent
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "进入创建类");//指定快捷方式名称
Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_like_5);//快捷方式图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
shortcut.putExtra("duplicate", false);//不允许重复创建
sendBroadcast(shortcut);
Toast.makeText(this, "创建快捷方式成功", Toast.LENGTH_SHORT).show();
就这样快捷方式创建成功了
第二个问题:如果我已经有了快捷方式,然后我需要判断一下是否存在
看了网上各种判断存在方法,想法是对的,就是去找存储快捷方式的表,但是写的太死了,毕竟rom是会被定制的,例如小米的miui,表的位置就不在网上写的那些地址下
所以呢,我们换一个思维,如果我要去读取这个表,我需要一个权限,也可以说就是这个表据要这个权限的用户才可以读,所以跟据这个思维那么我们就可以查找一下整个安装的应用中提供要这个权限才能读的应用信息是什么,然后就知道这个表确切位置是在哪了
代码很简单
private boolean checkHasShortCutBefore() {
boolean isInstallShortcut = false;
final ContentResolver cr = this.getContentResolver();
String permission = "com.android.launcher.permission.READ_SETTINGS";//拥有读取setting权限的应用
List<PackageInfo> packs = this.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);//找出所有已安装并提供服务的包信息
if (packs == null) {
return false;
}
String tempAuthority = null;
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {//遍历提供
if (permission.equals(provider.readPermission) || permission.equals(provider.writePermission)) {
//如果该提供者的读或者写权限跟我们申请的权限相同,则确定了位置
tempAuthority = provider.authority;
}
}
}
}
final String AUTHORITY = tempAuthority == null ? "com.android.launcher.settings" : tempAuthority;//默认在这个地址com.android.launcher.settings
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");//组装表的uri
try {//加异常捕获是因为有些表名居然不叫favorites,这种很纠结
Cursor c = cr.query(CONTENT_URI, new String[]{"title", "iconResource"}, "title=?", new String[]{"进入创建类"}, null);//注意名字是跟我上面创建的名称一致
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
c.close();
}
}catch (Exception e){
}
return isInstallShortcut;
}
好了,本文结束,如有疑问可以邮件联系E-MAIL:iamwsbear@gmail.com
如果你验证了觉得好用,欢迎点赞
如需转载,必须带上本人邮件地址,否则视为侵权