1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android自动创建桌面 Android创建桌面快捷方式

android自动创建桌面 Android创建桌面快捷方式

时间:2020-04-24 22:50:19

相关推荐

android自动创建桌面 Android创建桌面快捷方式

需求:点击按钮创建快捷方式

1.用户触发创创建事件时,在手机桌面创建指定页面的快捷方式。

2.当APP关闭时,点击桌面快捷方式打开APP,跳转至指定页面

3.当APP在后台是,点击桌面快捷方式,跳转至指定页面

分析:

Android中通过本地广播的方式将创建快捷方式的命令发送给手机,让手机在桌面执行快捷方式创建操作;Intent作为携带信息的载体。

效果图:

Shortcut1.gif

实现:

1. 配置权限

2. 代码实现

1、创建携带快捷方式信息的intent

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

2、设置不允许重复创建

shortCutIntent.putExtra("duplicate", false);

3、设置快捷方式的名字

shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "测试");

4、设置快捷方式的图标

shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));

5、设置设置关联程序

Intent intent = new Intent();

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

intent.setClass(context, MainActivity.class);

shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

// 发送广播

context.sendBroadcast(shortCutIntent);

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