[英]how to create app shortcut on home screen on Android phones on installation
[英]How to create a Shortcut on the Home Screen in Android 8?
我将在android 8的主屏幕上创建快捷方式,并使用ShortcutManager
在上下文菜单中创建快捷方式,用户应在主屏幕上手动拖动快捷方式,而在主屏幕上自动创建的下部Android 8中,我的代码是:
ShortcutManager sM = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent intent1 = new Intent(getApplicationContext(), MasterActivity.class);
intent1.setAction(Intent.ACTION_VIEW);
ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this, "shortcut1")
.setIntent(intent1)
.setLongLabel("Ayande")
.setShortLabel("This is the Ayandeh")
.setDisabledMessage("Login to open this")
.setIcon(Icon.createWithResource(this, R.drawable.ayandeh))
.build();
sM.addDynamicShortcuts(Arrays.asList(shortcut1));
结果:
但是结果我要自动创建快捷方式:
弄清楚了。 使用ShortcutManager.requestPinShortcut。 但是请注意,该方法将打开一个对话框,询问用户是否将快捷方式添加到主屏幕。 对我来说不幸的是,这个对话是在其他活动之后打开的,所以从来没有看到过!
您可以使用此方法
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.