Android创建桌面快捷方式

来源:互联网 发布:哪个电视直播软件最好 编辑:程序博客网 时间:2024/06/06 05:54

Android创建桌面快捷方式

效果图

这里写图片描述

添加权限

<!-- 添加创建快捷方式的权限 --><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

配置快捷启动的Activity

在清单文件下,将要设置快捷启动的Activity添加intent-filter属性

  • AndroidManifest.xml

    <activity android:name=".Main2Activity">    <intent-filter>        <action android:name="android.intent.action.home" />        <category android:name="android.intent.category.DEFAULT" />    </intent-filter></activity>

创建快捷方式

/** * 创建快捷图标 * * @param view view */public void createShortcutIcon(View view) {    Toast.makeText(this, "创建快捷图标", Toast.LENGTH_SHORT).show();    String title = "第二页";    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);    Intent intent = new Intent(this, Main2Activity.class);    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    // 添加Intent    Intent createShortcutIconIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");    // 标题    createShortcutIconIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);    // 图标    createShortcutIconIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);    // Intent    createShortcutIconIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);    // 发送广播创建图标    sendBroadcast(createShortcutIconIntent);}
1 0
原创粉丝点击