我创建了一个测试 Activity ,该 Activity 在Android主屏幕上安装了自身的快捷方式。当您单击一个按钮时,该 Activity 应删除它刚创建的相同快捷方式。但是,我似乎没有采取任何措施删除快捷方式。
这是Java代码(ShortcutTest.java):
import java.net.URISyntaxException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortcutTest extends Activity {
String shortcutUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addShortcut(getBaseContext());
Button button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
removeShortcut(getBaseContext());
finish();
}
});
}
public void addShortcut(Context context) {
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.telespree.android.client", "com.telespree.android.client.ShortcutTest");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutTest");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutUri = intent.toUri(MODE_WORLD_WRITEABLE);
context.sendBroadcast(intent);
}
public void removeShortcut(Context context) {
Intent intent = null;
try {
intent = Intent.parseUri(shortcutUri, 0);
} catch (URISyntaxException e) {
}
intent.setAction("com.android.launcher.permission.UNINSTALL_SHORTCUT");
context.sendBroadcast(intent);
}
}
这是 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.telespree.android.client"
android:versionCode="1"
android:versionName="1.0">
<permission
android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="normal"
/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ShortcutTest"
android:label="@string/app_name" android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<!--
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
-->
<uses-sdk android:minSdkVersion="7" />
</manifest>
我几乎肯定存在某种权限问题,尽管我在Internet上也看到过其他帖子指出这应该可行。
任何意见是极大的赞赏。
谢谢。
最佳答案
已淘汰;只保留历史记录
该答案发布于2014年,当时所描述的方法依赖于大多数Android设备中存在的功能。但是,正如Adrian-Costin Țundrea所提到的,此功能was removed是几年前来自Launcher3的,它是AOSP启动器,Google Now Launcher就是1所基于的。提交消息说:
截至2017年3月,该启动器也称为is being phased out,它支持“Google搜索启动器服务”,这意味着制造商可以将某个Google库集成到自己的自定义启动器中,而不必依赖于Google提供的标准化启动器。
考虑到每个制造商都可以随意使用他们想要的任何方式来实现自己的启动器,并且假设其中一些是基于Launcher3的,因此很难确定下面的方法将在哪些设备上运行,因为Launcher3甚至可以在某些Android 4.1设备上运行,在oldest devices still in use中。
问候!
我刚刚处理了同样的确切问题,并希望在成功解决此问题后分享我的经验。 tl; dr-跳至下面的“结论”。
一些背景:
在处理应用程序的“下一个版本”时,出现了更改默认入口点(即重命名“主要 Activity ”)的需求。对此不满意,因为要从旧版本升级的用户仍将具有旧的快捷方式,指向错误的位置。为了尽可能避免出现问题,在首次启动时(他们并不知道),将旧的快捷方式替换为新的快捷方式。
步骤1:建立新的入口点
这是最简单的部分。要声明入口点,要做唯一必要的事情就是将以下<action ...>
标记放入 list 中的相应 Activity 声明中:
<activity
android:name="YOUR_PACKAGE_NAME.YOUR_ACTIVITY_NAME"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
从某种意义上说,使入口点成为默认值的原因是启动器快捷方式指向该入口点。这就是为什么开发人员通常也将其包含在
<intent-filter>
中的原因:<category android:name="android.intent.category.LAUNCHER"/>
应该注意的是,在
<intent-filter>
中具有此功能的每个 Activity 都会在您的应用程序抽屉中创建一个项目-这就是为什么在大多数情况下只需要1个实例的原因。步骤2:找出旧捷径的运作方式
拥 Root设备后,我可以访问存储启动器/主屏幕/桌面项的数据库表(see image of what the SQLite entries looks like),它位于:
/data/data/com.android.launcher/databases/launcher.db -> SELECT * FROM favorites`
这是图像中突出显示的条目的可读性更高的版本:
#Intent;
action=android.intent.action.MAIN;
category=android.intent.category.LAUNCHER;
launchFlags=0x10200000;
package=gidutz.soft.bluecard;
component=gidutz.soft.bluecard/.LoadingScreen;
end
请注意
0x10200000
-这在步骤4-尝试以下1 中进行了说明。步骤3:找出快捷方式卸载程序的期望
UninstallShortcutReceiver.java
中的第38-42行告诉我们: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) { ... }
这意味着“卸载意图”必须同时具有
Intent.EXTRA_SHORTCUT_INTENT
和Intent.EXTRA_SHORTCUT_NAME
的,否则它甚至不会考虑执行。步骤4:找到正确的语法
这是一个试验的错误,结局是错误的。
尝试1:重构意图
Intent oldShortcutIntent = new Intent();
oldShortcutIntent.setAction(Intent.ACTION_MAIN);
oldShortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
oldShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED +
Intent.FLAG_ACTIVITY_NEW_TASK);
oldShortcutIntent.setPackage("gidutz.soft.bluecard");
oldShortcutIntent.setComponent(new ComponentName("gidutz.soft.bluecard",
".LoadingScreen"));
// The above line is equivalent to:
Intent oldShortcutIntent = new Intent(getApplicationContext(),LoadingScreen.class);
Intent uninstaller = new Intent();
uninstaller.putExtra(Intent.EXTRA_SHORTCUT_INTENT, oldShortcutIntent);
uninstaller.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
uninstaller.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(uninstaller);
结果:图标未删除。
0x10200000
实际上是两个参数之和,如here所述。尝试2:使用viralpatel的原样代码
Intent shortcutIntent = new Intent(getApplicationContext(),LoadingScreen.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
结果:图标未删除。
尝试3:“蛮力”
尝试复制意图,使其完全与launcher.db中显示的意图相同:
Intent intent = new Intent();
String oldShortcutUri = "#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;package=gidutz.soft.bluecard;component=gidutz.soft.bluecard/.LoadingScreen;end";
try {
Intent altShortcutIntent = Intent.parseUri(oldShortcutUri,0);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, altShortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Blue Card");
} catch (URISyntaxException e) {
}
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
结果:图标已删除!!
综上所述
launcher.db
获得的URI,确保您的“Icon Uninstaller”意图使用与用于创建要删除的图标的URI完全相同的URI。 资料来源
1)This guide at viralpatel.net
2)Google's implementation of UninstallShortcutReceiver.java
3)This thread at xdadevelopers
P.S.
为了模拟和调试Google Play更新(保留了旧的快捷方式),我执行了以下操作:
注意1:回顾起来,使用从数据库中获取的URI手动创建旧的快捷方式可能要容易得多,而不需要经历所有备份和强制停止的考验。
注意2:我没有尝试使用此方法删除属于其他应用程序的图标,但是这样做可能太疯狂了。