您的位置:首页 > 移动开发 > Android开发

删除其他应用的快捷方式

2014-05-16 21:09 211 查看
一般应用创建的快捷方式可应用是绑定的,应用卸载后,该快捷方式自然会被清理掉。但是有些人有意或无意地不关联快捷方式和应用本身,导致应用被卸载后,快捷方式依然存在,用户不小心点击后,可能会跳到广告页面下载东西。

所以昨天看了下如何卸载其他应用创建的快捷方式。

1、如何创建快捷方式

创建快捷方式的代码这里不贴出来,主要是讲下系统是如何管理快捷方式的。

首先快捷方式肯定是由launcher管理,创建后,launcher维护一个数据库data/databases/launcher.db。数据库格式如下:



添加快捷方式后,数据如下:



很重要的一点,这个数据库会记录创建快捷方式的应用包名iconPackage、快捷方式名title、以及intent。

2、launcher如何卸载快捷方式

大家应该知道,卸载快捷方式只需要发送个广播,那launcher接收到广播怎么做的呢?

private static void removeShortcut(Context context, Intent data,
final SharedPreferences sharedPrefs) {
Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);

if (intent != null && name != null) {
final ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);

final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);

boolean changed = false;

try {
while (c.moveToNext()) {
try {
if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
final long id = c.getLong(idIndex);
final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
cr.delete(uri, null, null);
changed = true;
if (!duplicate) {
break;
}
}
} catch (URISyntaxException e) {
// Ignore
}
}
} finally {
c.close();
}launcher会根据title去launcher.db中查寻对应的intent,利用intent.filterEquals函数对接收到的intent和查询的intent对比,满足就可以卸载快捷方式了。
filterEquals代码如下:

public boolean filterEquals(Intent other) {
if (other == null) {
return false;
}
if (mAction != other.mAction) {
if (mAction != null) {
if (!mAction.equals(other.mAction)) {
return false;
}
} else {
if (!other.mAction.equals(mAction)) {
return false;
}
}
}
if (mData != other.mData) {
if (mData != null) {
if (!mData.equals(other.mData)) {
return false;
}
} else {
if (!other.mData.equals(mData)) {
return false;
}
}
}
if (mType != other.mType) {
if (mType != null) {
if (!mType.equals(other.mType)) {
return false;
}
} else {
if (!other.mType.equals(mType)) {
return false;
}
}
}
if (mPackage != other.mPackage) {
if (mPackage != null) {
if (!mPackage.equals(other.mPackage)) {
return false;
}
} else {
if (!other.mPackage.equals(mPackage)) {
return false;
}
}
}
if (mComponent != other.mComponent) {
if (mComponent != null) {
if (!mComponent.equals(other.mComponent)) {
return false;
}
} else {
if (!other.mComponent.equals(mComponent)) {
return false;
}
}
}
if (mCategories != other.mCategories) {
if (mCategories != null) {
if (!mCategories.equals(other.mCategories)) {
return false;
}
} else {
if (!other.mCategories.equals(mCategories)) {
return false;
}
}
}

return true;
}很简单,就是获取相关属性对比下就OK了。
3、我们如何卸载其他应用的快捷方式

鉴于launcher的过滤规则不是很严,并且launcher.db是可访问的,我们只需要读取数据库中包对应的title和intent,重新构造一个新intent,发送广播就行了。代码如下:

String iconPackage = cursor.getString(cursor.getColumnIndex("iconPackage"));
if (!(iconPackage == null) && iconPackage.equals(packageName)) {
String title = cursor.getString(cursor.getColumnIndex("title"));
String intent = cursor.getString(cursor.getColumnIndex("intent"));

Intent oldIntent = Intent.parseUri(intent,0);
Intent newIntent = new Intent();
newIntent.setAction(oldIntent.getAction());
newIntent.setType(oldIntent.getType());
newIntent.setPackage(oldIntent.getPackage());
newIntent.setComponent(oldIntent.getComponent());
newIntent.setData(oldIntent.getData());
Set<String> cats = oldIntent.getCategories();
if ( !(cats == null)) {
for (Iterator<String> it = cats.iterator(); it.hasNext();) {
newIntent.addCategory(it.next().toString());
}
}
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,newIntent);
context.sendBroadcast(shor
4000
tcut);
return true;
}4、小结
经测试,可以卸载其他应用的快捷方式,不要做坏事哦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android shortcut