参考
创建桌面快捷方式需权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
代码实现
/**
* 创建快捷方式
* @param cxt Context上下文
* @param icon 图标
* @param title 标题
* @param args 附加参数
* @param cls 目标Activity,注意,这里最好用LancherActivity,否则兼容性不好
*/
public static void createDesktopShortCut(Context cxt, int icon, String title,Bundle args,Class<?> cls) {
// 创建快捷方式的Intent
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重复创建
shortcutIntent.putExtra("duplicate", false);
// 需要现实的名称
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 快捷图片
ShortcutIconResource ico = Intent.ShortcutIconResource.fromContext(cxt.getApplicationContext(), icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
Intent intent = new Intent(cxt, cls);
// 下面两个属性是为了当应用程序卸载时桌面上的快捷方式会删除
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
//可以带参数,点击快捷方式进入Activity时,一般可以从onStart中读取该参数
intent.putExtra("arguments", args);
// intent.setSourceBounds(Rect);设置在屏幕上的坐标
// 点击快捷图片,运行的程序主入口(部分APP需要通过主页面进入)
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
// 发送广播。OK
cxt.sendBroadcast(shortcutIntent);
Log.e("ShortCutIcon", "Create");
}
/**
* 删除快捷方式
* @param cxt 上下文
* @param cls 目标Activity
* @param shortcutName 快捷方式名称
*/
public void deleteShortCut(Context cxt,Class cls,String shortcutName)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
/**改成以下方式能够成功删除,估计是删除和创建需要对应才能找到快捷方式并成功删除**/
Intent intent = new Intent();
intent.setClass(cxt, cls);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
cxt.sendBroadcast(shortcut);
}
/**
* 检测快捷方式是否已经创建
* @param cx 上下文
* @param shortcutName 快捷方式名称
* @return
*/
public static boolean hasShortcut(Context cx,String shortcutName)
{
boolean result = false;
// 获取当前应用名称
final String uriStr;
if (android.os.Build.VERSION.SDK_INT < 8) {
uriStr = "content://com.android.launcher.settings/favorites?notify=true";
} else {
uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
}
final Uri CONTENT_URI = Uri.parse(uriStr);
final Cursor c = cx.getContentResolver().query(CONTENT_URI, null, "title=?", new String[] { shortcutName }, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
另外,我们可能需要对部分Icon进行一定的处理,显然通过资源名称读取图片到快捷方式具有局限性,比如如下方案
// 快捷图片
ShortcutIconResource ico = Intent.ShortcutIconResource.fromContext(cxt.getApplicationContext(), icon);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
我们需要进行图片处理,处理后的图片如果不存入MediaStore显然不具有资源名称,因此,我么可以选择如下方案
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newBitmap);
Bitmap实现了Parcelable,因此这种方案是合理的
监听快捷方式的安装和卸载
创建监听广播
public class ShortCutBroadCastReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("ShortCutBroadCastReciver", intent.getAction());
}
}
注册监听
广播可以被注册多次,前提是如果你有多个IntentFilter
private void registerBroadCast()
{
shortCutBroadCastReciver = new ShortCutBroadCastReciver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(INSTALL_SHORTCUT_ACTION);
intentFilter.addAction(UNINSTALL_SHORTCUT_ACTION);
registerReceiver(shortCutBroadCastReciver, intentFilter);
IntentFilter intentFilte2 = new IntentFilter();
intentFilte2.addAction(PACKAGE_ADDED_ACTION);
intentFilte2.addAction(PACKAGE_REMOVED_ACTION);
intentFilte2.addDataScheme("package");
registerReceiver(shortCutBroadCastReciver, intentFilte2);
}