gpt4 book ai didi

java - 在 Android O 的主屏幕上创建快捷方式

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:31 35 4
gpt4 key购买 nike

自 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 替换为 {@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());

我收到必须提供快捷方式 Intent 的错误:

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 的回答中所建议的,我设置了 Intent 并得到了 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);

这会弹出权限对话框,如下所示:

Permisison dialog

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

最佳答案

最后加上这个

shortcutManager.requestPinShortcut ( shortcutInfo ,  null )

并为 shortcutInfo 检查设置唯一的 Id

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

👍

关于java - 在 Android O 的主屏幕上创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46796674/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com