繁体   English   中英

如何在安装后自动创建快捷方式?

[英]How To Create Shortcut Automatically After Installation?

如何在安装后自动创建快捷方式? 下面的代码不起作用!

 private void createShortCut() {
     Intent intent = new Intent(ACTION_INSTALL_SHORTCUT);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
     intent.putExtra("duplicate", false); 
     Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//icon
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , MainActivity.class));

     setResult(RESULT_OK, intent);
     sendBroadcast(intent);
 }

安装后无法自动创建快捷方式,因为安装后代码不会自动运行。

是的,无论是您的应用程序还是任何第三方应用程序或系统应用程序,都可以创建快捷方式。 请遵循此示例代码。

  try
        {
            for (int i = 0; i < packageNameArray.length; i++)
            {
                Intent shortcutIntent = Util.makeIntent(MyPreference.getAction(context), MyPreference.getCategory(context), packageNameArray[i], ActivityNameArray[i]);
                Bitmap shortcutIcon2 = Util.confleteBitmap((UtilityClass.StringToBitMap(IconArray[i])), BitmapFactory.decodeResource(context.getResources(), R.drawable.shortcut_mark));
                data.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
                data.putExtra("android.intent.extra.shortcut.ICON", shortcutIcon2);
                data.putExtra("android.intent.extra.shortcut.NAME", AppNameArray[i]);
                //uninstalling shortcut to remove duplicacy
                Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
                intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                intent.putExtra("duplicate", false);
                intent.putExtras(data);
                context.sendBroadcast(intent);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

这里packagenamearray是您选择创建快捷方式的所有应用程序,如果您只有要创建的应用程序快捷方式,则会在此处传递应用程序包名称 activitynamearray是所选应用程序的活动名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM