[英]Home screen shortcut wont add
我试图以编程方式在主屏幕上添加一个短片,但它不起作用。 该代码适用于奥利奥及以上,但不适用于以下。
public void addShortcutToHomeScreen() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
.setIntent(new Intent(this, ActivityMemes.class).setAction(Intent.ACTION_MAIN)) //!!! intent's action must be set on oreo
.setShortLabel("Memeify")
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
} else {
Intent shortcutIntent = new Intent(getApplicationContext(),
Activity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
}
这是我的清单文件。
<activity
android:name="com.my.package.Activities.Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
请告诉我我做错了什么,因为我试图修复它但失败了。 还有比这更好的方法吗? 任何帮助将不胜感激谢谢。
编辑 :
在 Logcat 中找到了这个。
2019-01-16 13:27:46.773 735-13377/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } from com.chameleon.memeify (pid=14824, uid=10300) requires com.android.launcher.permission.INSTALL_SHORTCUT due to receiver com.sec.android.app.launcher/com.android.launcher3.home.InstallShortcutReceiver
你可以在你的应用程序类中使用这个方法
public void createShortCut() {
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.drawable.logo);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
sendBroadcast(shortcutintent);
}
}
并应用此权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
如果您使用的是 Android 8+,则不能再使用广播,必须使用 ShortcutManager : https : //developer.android.com/about/versions/oreo/android-8.0-changes#as
Android 8.0(API 级别 26)包括对应用快捷方式的以下更改:
- com.android.launcher.action.INSTALL_SHORTCUT 广播不再对您的应用产生任何影响,因为它现在是一个私有的隐式广播。 相反,您应该使用 ShortcutManager 类中的 requestPinShortcut() 方法创建应用程序快捷方式。
- ACTION_CREATE_SHORTCUT 意图现在可以创建您使用 ShortcutManager 类管理的应用程序快捷方式。 此意图还可以创建不与 ShortcutManager 交互的旧版启动器快捷方式。 以前,此意图只能创建旧版启动器快捷方式。
- 使用 requestPinShortcut() 创建的快捷方式和在处理 ACTION_CREATE_SHORTCUT 意图的活动中创建的快捷方式现在是成熟的应用快捷方式。 因此,应用程序现在可以使用 ShortcutManager 中的方法更新它们。
- 旧版快捷方式保留了之前 Android 版本的功能,但您必须在应用中手动将其转换为应用快捷方式。 要了解有关应用程序快捷方式更改的更多信息,请参阅固定快捷方式和小部件功能指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.