【问题标题】:Create shortcut on home screen in Android O在 Android O 的主屏幕上创建快捷方式
【发布时间】:2018-03-29 12:44:05
【问题描述】:

自 Android O 起,不推荐使用 com.android.launcher.action.INSTALL_SHORTCUT。在以前的版本中,我使用了它并且它有效。

Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), MainActivity.class));
sendBroadcast(shortcutintent);

但现在这不再起作用了。没有创建主屏幕快捷方式。如何在 Android O 中创建主屏幕快捷方式?在源代码中它说@deprecated Replaced with {@link android.content.pm.ShortcutManager#createShortcutResultIntent}。所以我尝试了这个:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfo.setShortLabel(getString(R.string.app_name));
mShortcutInfo.setLongLabel(getString(R.string.app_name));
mShortcutInfo.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
shortcutManager.createShortcutResultIntent(mShortcutInfo.build());

我得到了必须提供快捷方式意图的错误:

10-17 23:08:00.305 13256-13256/com.audiorecorder.wel.voicerecorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.wel.shortcut, PID: 13256
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wel.shortcut/com.wel.shortcut.MainActivity}: java.lang.NullPointerException: Shortcut Intent must be provided
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                  Caused by: java.lang.NullPointerException: Shortcut Intent must be provided
                                                                                     at android.os.Parcel.readException(Parcel.java:1948)
                                                                                     at android.os.Parcel.readException(Parcel.java:1888)
                                                                                     at android.content.pm.IShortcutService$Stub$Proxy.createShortcutResultIntent(IShortcutService.java:635)
                                                                                     at android.content.pm.ShortcutManager.createShortcutResultIntent(ShortcutManager.java:1043)
                                                                                     at voicerecorder.wel.audiorecorder.com.voicerecorder.MainActivity.onCreate(MainActivity.java:80)
                                                                                     at android.app.Activity.performCreate(Activity.java:6975)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                     at android.os.Looper.loop(Looper.java:164) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

编辑: 正如 ianhanniballake 的回答中所建议的那样,我设定了意图并得到了java.lang.NullPointerException: intent's action must be set 所以我尝试了new Intent("com.android.launcher.action.INSTALL_SHORTCUT")。代码运行但没有创建快捷方式。

编辑 2: 这是我现在正在运行的代码,但我在主屏幕上看不到快捷方式。

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfo.setShortLabel(getString(R.string.app_name));
mShortcutInfo.setLongLabel(getString(R.string.app_name));
mShortcutInfo.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_CREATE_SHORTCUT);
shortcutIntent.putExtra("duplicate", false);
mShortcutInfo.setIntent(shortcutIntent);
sendBroadcast(shortcutManager.createShortcutResultIntent(mShortcutInfo.build()));

编辑 3:

ShortcutInfo.Builder mShortcutInfoBuilder = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfoBuilder.setShortLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setLongLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_CREATE_SHORTCUT);
mShortcutInfoBuilder.setIntent(shortcutIntent);
ShortcutInfo mShortcutInfo = mShortcutInfoBuilder.build();
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
mShortcutManager.requestPinShortcut(mShortcutInfo, null);

这会打开权限对话框,如下所示:

但问题是它没有出现在应用程序前台。它仅在按下返回键后出现。它也不会出现在主页键上。

【问题讨论】:

  • 你设置权限了吗?
  • 是的,<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  • 您是否能够找到在 Android Oreo 及更高版本上不显示对话框的情况下创建快捷方式的解决方案?

标签: java android shortcut homescreen android-8.0-oreo


【解决方案1】:

在最后添加

shortcutManager.requestPinShortcut ( shortcutInfo ,  null )

对于shortcutInfo 检查设置唯一ID

ShortcutInfo.Builder mShortcutInfo = new ShortcutInfo.Builder(MainActivity.this, **getString(R.string.Different_String)**);

?

【讨论】:

  • 请查看编辑。我已经添加了这个,但是有一个问题。
  • @Ali android O 上的快捷方式有限制。尝试调用finish onPause()
  • 我不希望用户离开应用
【解决方案2】:

我知道这个帖子很古老,但它是您在谷歌上搜索弃用的第一个结果,所以我认为这可能对其他人有所帮助:

提问者在编辑 3 中的 setIntent 中设置了错误的意图。您应该指定在按下快捷方式时调用的意图,而不是 Intent.ACTION_CREATE_SHORTCUT 意图。 ianhanniballake 已经指出了这一点。

编辑 3 的修复如下所示:

ShortcutInfo.Builder mShortcutInfoBuilder = new ShortcutInfo.Builder(MainActivity.this, getString(R.string.app_name));
mShortcutInfoBuilder.setShortLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setLongLabel(getString(R.string.app_name));
mShortcutInfoBuilder.setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher));
mShortcutInfoBuilder.setIntent(new Intent(getApplicationContext(), MainActivity.class));
ShortcutInfo mShortcutInfo = mShortcutInfoBuilder.build();
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
mShortcutManager.requestPinShortcut(mShortcutInfo, null);

createShortcutResultIntent 实际上是用于一些非常不同的东西,它是用于当您想要在创建固定快捷方式成功时调用回调函数。

【讨论】:

    【解决方案3】:

    你可以使用这个方法

    private void createShortcut() {
    ShortcutManager shortcutManager = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
        shortcutManager = mContext.getSystemService(ShortcutManager.class);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (shortcutManager != null) {
            if (shortcutManager.isRequestPinShortcutSupported()) {
                ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, uniqueid)    
                        .setShortLabel("Demo")
                        .setLongLabel("Open the Android Document")
                        .setIcon(Icon.createWithResource(mContext, R.drawable.andi))
                        .setIntent(new Intent(Intent.ACTION_VIEW,
                                Uri.parse("https://stackoverflow.com")))
                        .build();
    
                shortcutManager.requestPinShortcut(shortcut, null);
            } else
                Toast.makeText(mContext, "Pinned shortcuts are not supported!", Toast.LENGTH_SHORT).show();
        }
    }}
    

    我在我的应用程序中使用它,但只有一个限制它只会在 MAX 处创建两个快捷方式 不知道为什么如果有人找到创建更多的方法,请在此处提及..

    【讨论】:

      【解决方案4】:

      您必须致电setIntent():

      mShortcutInfo.setIntent(new Intent(getApplicationContext(), MainActivity.class));
      

      【讨论】:

      • 我调用了 setIntent()。代码运行但未创建快捷方式。
      • 重要提示:您必须在 Intent 上添加一个操作,否则它将崩溃:java.lang.NullPointerException:必须设置意图的操作
      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多