android 在launcher上创建快捷方式shortcut

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuxingchong/article/details/80980781

Shortcut 是谷歌在API25提出来的 ,通过长按应用图标弹出快捷方式,如下图所示:


有两种方式可以添加快捷方式,一种是静态添加通过长按应用图标弹出显示

AndroidManifest.xml中配置要启动的程序的Activity<intent-filter>元素即可。

    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

<activity
    android:name=".gesture.CreateGestureShortcutActivity"
    android:autoRemoveFromRecents="true"
    android:label="@string/gesture_select_actions"
    android:resizeableActivity="false"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.DayNight">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

另一种通过发送广播动态添加

//向桌面添加快捷方式的广播
Intent addShortCutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//其次,通过为该Intent添加Extra属性来设置快捷方式的标题、图标及快捷方式对应的启动程序(分别对应下面的代码);
Parcelable icon = Intent.ShortcutIconResource.fromContext(CreateGestureShortcutActivity.this, R.drawable.icon_gesture);
//用于点击快捷方式要启动的程序,这里就启动本程序了
Intent startIntent = new Intent(AddShortCutActivity.this, AddShortCutActivity.class);
//快捷方式的名称
addShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手势");
//快捷方式的图标
addShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//将快捷方式与要启动的程序关联起来
addShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, startIntent);
//最后,就是调用sendBroadcast()方法发送广播即可添加快捷方式。
sendBroadcast(addShortCutIntent);

最后注意需要添加权限

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

猜你喜欢

转载自blog.csdn.net/zhuxingchong/article/details/80980781
今日推荐