在Android O中在主屏幕上创建快捷方式

时间:2017-10-17 18:17:38

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

由于Android O,不推荐使用RxJava。在之前的版本中,我使用了它并且它有效。

com.android.launcher.action.INSTALL_SHORTCUT

但现在这不再适用了。没有创建主屏幕快捷方式。如何在Android O中创建主屏幕快捷方式?在源代码中,它显示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); 。所以我尝试了这个:

@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());

编辑: 正如ianhanniballake的回答所示,我设定了意图并获得了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)  所以我尝试了java.lang.NullPointerException: intent's action must be set。代码运行但没有创建快捷方式。

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

new Intent("com.android.launcher.action.INSTALL_SHORTCUT")

编辑3:

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()));

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

Permisison dialog

但问题是它没有出现在应用前景中。仅在按后退键后才会出现。它也不会出现在主页按键上。

4 个答案:

答案 0 :(得分:2)

在最后添加

shortcutManager.requestPinShortcut ( shortcutInfo ,  null )

并且用于shortcutInfo检查以设置唯一ID

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

答案 1 :(得分:1)

您可以使用此方法

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处仅创建两个快捷方式 不知道为什么没有人找到创建更多内容的方法,请在这里提及。

答案 2 :(得分:1)

我知道此线程很古老,但它是您在Google上弃用Google的第一个结果,因此我认为这可能对其他人有帮助:

询问的人在Edit 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 :(得分:0)

您必须致电setIntent()

mShortcutInfo.setIntent(new Intent(getApplicationContext(), MainActivity.class));
相关问题