Hun*_*r D 8 android shortcut manifest android-intent android-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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是清单:
<?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>
Run Code Online (Sandbox Code Playgroud)
我几乎肯定有某种权限问题,虽然我在互联网上看到其他帖子表明这应该是可能的.
任何意见是极大的赞赏.
谢谢.
Dev*_*-iL 20
这个答案发布于2014年,当时描述的方法依赖于大多数Android设备中存在的功能.然而,正如Adrian-CostinŢundrea所提到的,几年前这个功能已经从Launcher3中删除了,而Launcher3是Google Now Launcher所基于的AOSP启动器1.提交消息说:
由于其设计的瑕疵而取消支持.删除快捷方式会导致完全重新加载.此外,我们没有任何所有者的概念,因此任何应用程序都可以删除任何快捷方式.
截至2017年3月,该发射器也正逐步淘汰,支持"谷歌搜索启动器服务",这意味着制造商可以将某个谷歌库集成到他们自己的自定义发射器中,而不是依靠谷歌提供的标准化发射器.
考虑到每个制造商都可以自由地以他们想要的方式实现他们的启动器,并且假设其中一些是基于Launcher3,很难分辨下面的方法将适用于哪些设备,因为Launcher3甚至可以在某些Android 4.1设备上运行,这是仍在使用的最古老的设备之一.
我刚刚处理了同样的问题,并希望在成功解决之后分享我的经验.tl; dr - 跳到下面的"结论".
在处理应用程序的"下一个版本"时,需要更改默认入口点(即重命名"主要活动").这是不赞成的,因为将从旧版本升级的用户仍将使用旧的快捷方式,指向错误的位置.为了尽可能避免出现问题,在他们不知情的第一次发射时,旧的捷径将被换成新的捷径.
这是最简单的部分.要声明一个入口点,唯一要做的就是将以下<action ...>标记放在Manifest中的相应活动声明中:
<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>
Run Code Online (Sandbox Code Playgroud)
在某种意义上,入口点默认是什么,是启动器快捷方式指向它.这就是为什么开发人员通常也将此包括在<intent-filter>:
<category android:name="android.intent.category.LAUNCHER"/>
Run Code Online (Sandbox Code Playgroud)
应该注意的是,每个具有此功能的活动<intent-filter> 都会在您的应用程序抽屉中创建一个项目 - 这就是为什么在大多数情况下,您只需要1个实例.
有了root设备,我可以访问存储启动器/主屏幕/桌面项目的数据库表(参见SQLite条目的图像),它位于:
/data/data/com.android.launcher/databases/launcher.db -> SELECT * FROM favorites`
Run Code Online (Sandbox Code Playgroud)
这是图像中突出显示的条目的更易读的版本:
#Intent;
action=android.intent.action.MAIN;
category=android.intent.category.LAUNCHER;
launchFlags=0x10200000;
package=gidutz.soft.bluecard;
component=gidutz.soft.bluecard/.LoadingScreen;
end
Run Code Online (Sandbox Code Playgroud)
注意0x10200000- 这将在步骤4 -下面的尝试1中解释.
第38-42行UninstallShortcutReceiver.java告诉我们:
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) { ... }
Run Code Online (Sandbox Code Playgroud)
意味着"卸载意图"必须同时具有这两者 Intent.EXTRA_SHORTCUT_INTENT,Intent.EXTRA_SHORTCUT_NAME否则它甚至不会考虑执行.
这是一个试错的案例,结局很快.
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);
Run Code Online (Sandbox Code Playgroud)
结果:图标未删除.这0x10200000实际上是这里解释的两个参数的总和.
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);
Run Code Online (Sandbox Code Playgroud)
结果:图标未删除.
尝试完全按照它在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);
Run Code Online (Sandbox Code Playgroud)
结果:图标已删除!!
launcher.db.2)Google的UninstallShortcutReceiver.java实现
为了模拟和调试Google Play更新(保留旧的快捷方式),我执行了以下操作:
注意1:回顾一下,使用从数据库获取的URI手动创建旧快捷方式可能更容易,而不是通过所有备份和强制停止测试.
注意2:我没有尝试使用此方法删除属于其他应用程序的图标,但它可能只是疯狂到可以工作.
Adr*_*rea 10
虽然Dev-iL和Funt的两个解决方案都建议他们这样做,直到Marshmallow.随着Android 6.0(具有Launcher v3),谷歌已经删除了UninstallShortcutReceiver,因为它存在安全问题(可能是因为它在这里显而易见).所以不要指望它适用于Android 6.0.希望在将来的某个版本中,它将以某种形式或其他形式进行读取.
PS:通常这应该是评论,但由于声誉,我不能发表评论......
小智 4
您需要设置操作,例如shortcutIntent:
shortcutIntent.setAction(Intent.ACTION_MAIN);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13879 次 |
| 最近记录: |