1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android添加快捷按钮 Android中创建快捷方式代码实例

android添加快捷按钮 Android中创建快捷方式代码实例

时间:2021-08-18 18:40:15

相关推荐

android添加快捷按钮 Android中创建快捷方式代码实例

1、添加权限(必须)

2、添加快捷键

public static void setupShortcut(Activity activity)

{

Intent shortcutIntent = new Intent(activity, MainActivity.class); //启动首页(launcher Activity)

Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");//快捷键名字可以任意,不过最好为app名称

Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

intent.putExtra("duplicate", false);//不允许重复创建

activity.sendBroadcast(intent);//发送广播创建快捷键

}

3、快捷键也可以指向非Launcher activity,只需要在AndroidManifest中对应的Activity 中添加如下配置

例如可以将2 中的MainActivity 改为任意其他Activity,同时在AndroidManifest中对应添加上述intent-filter就可以了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。