Android删除HomeScreen中的快捷方式
如何删除主屏幕中的快捷方式(以编程方式)。我可以使用意图,但没有其他现有的消除由我创建的快捷方式..Android删除HomeScreen中的快捷方式
Bitmap theBitmap = ((BitmapDrawable)icon).getBitmap();
Intent shortcutIntent = new Intent();
shortcutIntent.setAction(Intent.ACTION_VIEW);
shortcutIntent.setClassName(url, group);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
removeIntent.putExtra("duplicate", false);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, theBitmap);
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(removeIntent);
使用上述意图删除时
,可以删除快捷方式知道的图标包名+应用类名时。我们可以得到使用PackageInfo所有应用程序的图标包的名字,但没有推出类名
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.classname = p.applicationInfo.className;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
现在问题如下,请问有什么方法来获取应用程序的类名,那么我们就可以删除创建的快捷方式这个应用程序在主屏幕..任何帮助?
你不能从主屏幕中移除编程其他快捷方式
使用以下方法:
private void removeShortcutIcon(String app_name, Intent app_intent){
app_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
app_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, app_intent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, app_name);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(removeIntent);
}
,并得到下列方式安装的所有应用程序:
PackageManager pm = getPackageManager();
List<ResolveInfo> pacs_list = pm.queryIntentActivities(main_intent, 0);
for(int i=0; i < pacs_list.size(); i++)
{
pacs_list.get(i).loadIcon(pm);
pacs_list.get(i).activityInfo.packageName;
pacs_list.get(i).loadLabel(pm).toString();
}
注意:您可以从ResolveInfo获取各种属性。检查这个链接:ResolveInfo
这是可能的。
[更新]:由于版本的Android M,他们已经在AOSP Launcher3和谷歌现在启动的UNINSTALL_SHORTCUT意图完全去除的支持。目前没有替代或替代此API。 来源:https://code.google.com/p/android/issues/detail?id=179697
[更新2]:也许这是一些您有所帮助。我能够弄清楚,至少 - 卸载我的应用程序自己的主屏幕上的 shotscuts。尽管Google取消了对快捷方式的卸载支持,但一旦禁用了别名,指向活动别名的快捷方式仍将被删除。
小参考:
ComponentName yourAlias = new ComponentName(getPackageName(), "org.example.yourAliasName");
if (show){
flag = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}else{
flag = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
context.getPackageManager().setComponentEnabledSetting(yourAlias, flag, PackageManager.DONT_KILL_APP);
以下版本的Android M:
首先第一件事情,在这里我一再能够删除的Facebook快捷方式的代码, 无任何根(这是一样的op的代码,所以他是不是错误):
Intent appIntent = new Intent(Intent.ACTION_MAIN);
appIntent.setComponent(new ComponentName("com.facebook.katana", "com.facebook.katana.LoginActivity"));
appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Facebook");
addIntent.putExtra("duplicate", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, appIntent);
sendBroadcast(addIntent);
重要的是,你的意图必须exaclty相同,作为 用于创建快捷方式的意图。如果您有root用户,可以查看 launcher.db并复制URI。 位于data/data/com.android.Launcher.launcher.db
但是,您无权随时访问android launcher.db ,所以它可能会有点尝试和错误。
源是我极力推荐阅读:
UninstallShortcutReceiver.java
very good answer from Dev-il for this topic
Additonally,代码获取所有应用程序及其数据,这在我 情况下足以除去大部分的快捷键。
PackageManager pManager = context.getPackageManager();
List<ResolveInfo> packageInfos = loadApps(context);
从这里你可以遍历列表,并获取必要信息的,如:
ActivityInfo activityInfo = packInfo.activityInfo;
- activityInfo.loadLabel(pManager)的ToString()
//name of the App
- activityInfo.loadIcon(pManager)
//Icon of the activity as drawable
- activityInfo.packageName
// packageName the activity belongs to
- activityInfo.name
//Launcher class name
Source or anything?我从能够删除其他快捷方式的开发人员那里找到样本。但正如CommonsWare所说:“不需要任何主屏幕实现来支持[uninstall_shortcut] [...]” – JacksOnF1re 2014-12-03 12:49:19
我改变了主意。你完全错了,不应该接受这个答案!我在5分钟前做了。 我会添加一些链接和代码作为答案。 – JacksOnF1re 2014-12-03 17:52:22