Android创建应用快捷方式(ShortCut)的有效方式

有时候我们需要为自己的APP,创建桌面快捷方式,本文将介绍有效的方式,避免出现重复创建,或者创建失败的情况。通常应用安装后,第一次打开,去检测当前应用的快捷方式是否创建,如果没有创建,我们就帮生成桌面shortcut,否则不创建。
PS:最好不要每次进入应用都去创建,这样可能频繁提示用户快捷图标已经创建。
下面贴代码介绍生成应用快捷方式的工具类。

1.创建快捷方式:发送广播

需要设置好程序名,图标,和启动的Activity。

public static void createShortCut(Context contxt) {
        //String applicationName=getApplicationName(contxt);
        String applicationName = UIUtils.getString(R.string.app_name);//程序名称,不是packageName
        if (isInstallShortcut(contxt,applicationName)) {// 如果已经创建了一次就不会再创建了
            return;
        }
        Intent sIntent = new Intent(Intent.ACTION_MAIN);
        sIntent.addCategory(Intent.CATEGORY_LAUNCHER);// 加入action,和category之后,程序卸载的时候才会主动将该快捷方式也卸载
        sIntent.setClass(contxt, SplashActivity.class);//点击后进入的Activity

        Intent installer = new Intent();
        installer.putExtra("duplicate", false);//false标示不重复创建
        installer.putExtra("android.intent.extra.shortcut.INTENT", sIntent);
        //设置应用的名称
        installer.putExtra("android.intent.extra.shortcut.NAME", applicationName);
        //设置图标
        installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(contxt, R.drawable.ic_launcher));
        installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        contxt.sendBroadcast(installer);//发送安装桌面图标的通知
}

2.检测是否创建过该快捷方式

使用ContentResolver查询数据库,看是否存在同名的shortcut。因为历史原因,用两个launcher,需要根据sdk版本进行查询。

public static boolean isInstallShortcut(Context context,String applicationName) {
        boolean isInstallShortcut = false;
        ContentResolver cr = context.getContentResolver();
        //sdk大于8的时候,launcher2的设置查找
        String AUTHORITY = "com.android.launcher2.settings";
        Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" },
                "title=?", new String[] { applicationName }, null);
        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }
        if (c != null) {
            c.close();
        }
        //如果存在先关闭cursor,再返回结果
        if (isInstallShortcut) {
            return isInstallShortcut;
        }
        //android.os.Build.VERSION.SDK_INT < 8时
        AUTHORITY = "com.android.launcher.settings";
        CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
        c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?",
                new String[] {applicationName}, null);
        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }
        if (c != null) {
            c.close();
        }
        return isInstallShortcut;
    }

3.获取应用程序的名称:ApplicationName

如果上面的ApplicationName不是通过xml中的 String获取,也可以程序获取。

//获取当前app的应用程序名称
 public static String getApplicationName(Context  context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getApplicationContext().getPackageManager();                  applicationInfo=packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName =(String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

如何删除shortcut呢?

如果用户卸载我们的APP,通常快捷方式是会自动删除的,不需要自己处理。当然,我们程序也可以检测到已经有了快捷方式,删除它重新创建,其实原理如下:

//删除shortcut
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();
        } 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);
    }

未完待续,欢迎交流。杜工,Dusan,Q:291902259.

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Android 动态创建快捷方式的步骤: 1. 首先,您需要在 AndroidManifest.xml 文件中声明您的快捷方式。在应用程序的 <application> 标记内部,添加以下内容: ```xml <activity android:name=".MyShortcutActivity" android:label="@string/shortcut_label"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 2. 创建一个新的 Activity 类 MyShortcutActivity,该类将处理创建快捷方式的请求。在 onCreate() 方法中,您可以设置快捷方式的属性,例如快捷方式 ID、快捷方式标签和快捷方式图标。 ```java public class MyShortcutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置快捷方式 ID 和标签 String shortcutId = "my_shortcut"; String shortcutLabel = "My Shortcut"; // 创建快捷方式意图 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW); shortcutIntent.setClassName(this, MainActivity.class.getName()); // 创建快捷方式 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, shortcutId) .setShortLabel(shortcutLabel) .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon)) .setIntent(shortcutIntent) .build(); // 添加快捷方式 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); // 结束 Activity finish(); } } ``` 3. 在您的应用程序中,您可以通过调用 ShortcutManager 的 setDynamicShortcuts() 方法来添加动态快捷方式。在这个例子中,我们只添加了一个快捷方式,但您可以添加多个快捷方式。 ```java ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值