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(); }
参考资料:
1、https://juejin.im/entry/58e22183a22b9d005874a754#buildconfig-配置
2、https://developer.android.com/reference/android/content/pm/ShortcutManager.html
0 0
- Android 7.1 ShortCut
- Android-Shortcut
- Android创建快捷方式(shortcut)
- android 创建shortcut
- Android创建ShortCut
- android中shortcut
- Android Create Shortcut
- android shortcut 创建 判断
- android——shortcut
- Android创建快捷方式-SHORTCUT
- android快捷方式shortcut 管理
- Android 创建、删除ShortCut
- android studio shortcut
- Android 创建shortcut
- shortcut
- shortcut
- Android Shortcut操作(一)
- Android Shortcut操作(二)
- 银联全渠道平台接入
- 6-TCP 协议(序号和确认号)
- 安卓计时器的简单使用,按HH:MM:SS从0开始计时,以及从指定的时间开始计时
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- DOM中的NodeList与HTMLCollection
- Android 7.1 ShortCut
- 限制篇(5.4) 03. 控制下载和上传流量 ❀ 飞塔 (Fortinet) 防火墙
- 2955 Brackets
- C. Lucky Permutation----思维题
- Java释放资源
- [c++基础] 内存,堆栈,资源,异常处理相关问题
- java编写异步处理方法提升系统性能
- eclipse创建android项目出现error libz.so.1: cannot open shared object file:No such file or directory
- 题目1011:最大连续子序列