Android 7.1 ShortCut

来源:互联网 发布:则么安装vb 编辑:程序博客网 时间:2024/05/02 07:54

效果图:


1、配置

android {    compileSdkVersion 25     buildToolsVersion "25.0.0" // 或以上    defaultConfig {        targetSdkVersion 25    }}

第一种方式实现,静态注册

在 res/xml 文件夹底下创建一个xml

shortcut.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">    <shortcut        android:enabled="true"        android:icon="@drawable/ic_bar_detail_write"        android:shortcutDisabledMessage="@string/shortcut_publish"        android:shortcutId="publish"        android:shortcutLongLabel="@string/shortcut_publish"        android:shortcutShortLabel="@string/shortcut_publish">        <intent            android:action="android.intent.action.VIEW"            android:targetClass="com.james.shortcut.PublishPostActivity"            android:targetPackage="com.james.shortcut" />        <categories android:name="android.shortcut.conversation" />    </shortcut>    <shortcut        android:enabled="true"        android:icon="@drawable/logo"        android:shortcutDisabledMessage="@string/shortcut_write"        android:shortcutId="write"        android:shortcutLongLabel="@string/shortcut_write"        android:shortcutShortLabel="@string/shortcut_write">        <intent            android:action="android.intent.action.VIEW"            android:targetClass="com.james.shortcut.WriteActivity"            android:targetPackage="com.james.shortcut" />        <categories android:name="android.shortcut.conversation" />    </shortcut></shortcuts>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.james.shortcut">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <meta-data                android:name="android.app.shortcuts"                android:resource="@xml/shortcut" />        </activity>        <activity android:name=".PublishPostActivity"/>        <activity android:name=".WriteActivity"/>    </application></manifest>
strings.xml

<resources>    <string name="app_name">ShortCutDemo</string>    <string name="shortcut_publish">发布帖子</string>    <string name="shortcut_write">写帖子</string></resources>
详细代码下载地址:

http://download.csdn.net/detail/u013147860/9804288

第二种方式实现:动态实现

@RequiresApi(api = Build.VERSION_CODES.M)    public void register(View view){        ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);        List<ShortcutInfo> infos = new ArrayList<>();        // 按下返回按钮跳转的activity        Intent intent1 = new Intent(this, MainActivity.class);        intent1.setAction(Intent.ACTION_VIEW);        // 目标activity        Intent intent2 = new Intent(this, PublishPostActivity.class);        intent2.setAction("com.james.shortcut.BACK");        Intent[] intents = new Intent[2];        intents[0] = intent1;        intents[1] = intent2;        ShortcutInfo info = new ShortcutInfo.Builder(this,"publish-2")                .setShortLabel("动态创建-发布帖子")                .setLongLabel("动态创建-发布帖子")                .setIcon(Icon.createWithResource(this,R.drawable.ic_bar_detail_write))                .setIntents(intents)                .build();        infos.add(info);        mShortcutManager.setDynamicShortcuts(infos);    }


禁止快捷键:

 /**     * 动态删除     */    public void delete(View view) {        List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();        List<String> ids1 = new ArrayList<>();        for(ShortcutInfo infos : infos1){            ids1.add(infos.getId());        }        mShortcutManager.disableShortcuts(ids1,"已禁用");        mShortcutManager.removeDynamicShortcuts(ids1);        // 放在桌面的图标        List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();        List<String> ids2= new ArrayList<>();        for(ShortcutInfo info : infos2){            ids2.add(info.getId());        }        mShortcutManager.disableShortcuts(ids2);        mShortcutManager.removeAllDynamicShortcuts();    }


源码下载:http://download.csdn.net/detail/u013147860/9804354


参考资料:

1、https://juejin.im/entry/58e22183a22b9d005874a754#buildconfig-配置

2、https://developer.android.com/reference/android/content/pm/ShortcutManager.html



0 0
原创粉丝点击