本文实例讲述了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法。分享给大家供大家参考。具体实现方法如下: public static boolean has
本文实例讲述了Android添加(创建)、删除及判断是否存在桌面快捷方式的方法。分享给大家供大家参考。具体实现方法如下:
public static boolean hasshortcut(Context cx) {
boolean result = false;
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
}
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[] { title }, null);
if (c != null && c.getCount() > 0) {
result = true;
}
return result;
}
public static void delShortcut(Context cx) {
Intent shortcut = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test", "title:" + title);
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
cx.sendBroadcast(shortcut);
}
public static void addShortcut(Context cx) {
Intent shortcut = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = cx.getPackageManager()
.getLaunchIntentForPackage(cx.getPackageName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 获取当前应用名称
String title = null;
try {
final PackageManager pm = cx.getPackageManager();
title = pm.getApplicationLabel(
pm.getApplicationInfo(cx.getPackageName(),
PackageManager.GET_META_DATA)).toString();
Log.v("test", "title:" + title);
} catch (Exception e) {
}
// 快捷方式名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
// 不允许重复创建(不一定有效)
shortcut.putExtra("duplicate", false);
// 快捷方式的图标
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(cx, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
cx.sendBroadcast(shortcut);
}
希望本文所述对大家的Android程序设计有所帮助。
您可能感兴趣的文章:Android应用创建桌面快捷方式代码Android如何创建桌面快捷方式Android程序开发之手机APP创建桌面快捷方式解析Android应用启动后自动创建桌面快捷方式的实现方法Android 创建/验证/删除桌面快捷方式(已测试可用)android 为应用程序创建桌面快捷方式技巧分享Android编程实现向桌面添加快捷方式的方法Android编程添加快捷方式(Short)到手机桌面的方法(含添加,删除及查询)Android编程实现创建,删除,判断快捷方式的方法Android应用创建多个快捷方式Android实现向Launcher添加快捷方式的方法Android编程创建桌面快捷方式的常用方法小结【2种方法】
--结束END--
本文标题: Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
本文链接: https://www.lsjlt.com/news/26705.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0