在开发android项目中,遇到一个桌面快捷方式的bug,在部分samsung手机上,app覆盖安装后,以前创建的快捷方式图标和标题变成主程序的图标和标题。
一般创建桌面快捷方式是在应用程序中创建一个Intent,然后以Broadcast的形式通知Launcher修改数据库创建一个快捷方式,代码如下
public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
public void createLauncherShortcut() {
Intent shortcut = new Intent(ACTION_ADD_SHORTCUT);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "标题");
//设置图标
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),
// R.mipmap.baidu_shortcut));
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.baidu_shortcut);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
// // 是否允许重复创建
shortcut.putExtra("duplicate", false);
//
// //点击快捷方式的操作
Intent intent = new Intent("com.example.wolf.launchershortcut.action.shortcut");//自定义action
// 设置启动程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// intent.setClass(getApplicationContext(), MainActivity.class);
intent.setClassName(getApplicationContext(), "com.example.wolf.launchershortcut.MainActivity");
//广播通知桌面去创建
getApplicationContext().sendBroadcast(shortcut);
}
然后修改AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- 添加 intent-filter-->
<intent-filter>
<action android:name="com.example.wolf.launchershortcut.action.shortcut"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
快捷方式创建成功后,launcher会提示创建成功.
如果不自定义action,AndroidManifest.xml可以不用修改。
Launcher数据库 一般是在/data/data/的launcher包里的launcher.db文件。看里面的favorites表
itemType:应用程序用0表示,shortcut用1表示,folder用2表示,widget用4表示;container:应用程序的容器,-100表示在桌面的程序,-101表示是主屏底部的程序;
screen:在第几个屏;
cellX:在屏幕X轴的位置,(0,1,2,3),左上角为0点,往右依次增加;
cellY:在屏幕Y轴的位置,(0,1,2,3),左上角为0点,往下依次增加;
spallX:占X轴几个格;
spallY:占Y轴几个格;
title:应用程序的名字,有的title为空,表示是widget的快捷方式;
intent:启动这个图标的intent,当click的时候就会调用这个字段,启动相应的应用程序;
appWidgetId:-1表示不是widget,数字大于0表示是widget;
iconType:值为0表示图标的名字被定义为包名的资源id,值为1表示图标用bitmap保存;
重要的是intent这个字段,内容是
#Intent;action=android.intent.action.VIEW;component=com.example.wolf.launchershortcut/.MainActivity;end
component 组件 就是关系到 图标覆盖的元素。component 等于哪个activity,覆盖安装后就会取该activity的图标标题替换快捷方式的图标标题。在其他的大部分Launcher
里,component 关系到的是删除应用程序后,快捷方式也要删除,没有图标标题的事情,有问题的Launcher与之关联的元素是package,代码是intent.setPackage(getApplicationContext().getPackageName());
最后决定,Build.MANUFACTURER 为 samsung 的 设置package,其他的设置component,也就是代码里的 intent.setClassName(getApplicationContext(), "com.example.wolf.launchershortcut.MainActivity");
结果就是 覆盖安装后 图标标题改变的问题解决了,但是又多了个问题,大部分samsung手机删除程序后,快捷图标不能删除。
如果哪位大神知道更好的方法,麻烦告诉我,谢谢!