shortcuts的注册分为 静态和动态两种方式,和广播一样。写在manifest中,或者使用api动态去注册。
先看下 static 模式
在res/xml 新建一个xml文件,暂且叫shortcut.xml吧
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android/apk/res/android">
<shortcut
android:shortcutId="myInfo" //唯一的id
android:enabled="true" //是否启用
android:icon="@drawable/icon_myinfo" //图标
android:shortcutShortLabel="@string/myInfo_shortcut_short" //短label
android:shortcutLongLabel="@string/myInfo_shortcut_long" //长label,优先显示长label。显示不下,显示短label android:shortcutDisabledMessage="@string/myInfo_disabled_message">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.shortcut"
android:targetClass="com.example.shortcut.MyInfoActivity" />
<!--android.shortcut.conversation 描述当前的intent的执行环境 目前google官方提供的只有这个-->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="home"
android:enabled="true"
android:icon="@drawable/icon_home"
android:shortcutShortLabel="@string/home_shortcut_short"
android:shortcutLongLabel="@string/home_shortcut_long"
android:shortcutDisabledMessage="@string/home_disabled_message">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.shortcut"
android:targetClass="com.example.shortcut.HomeActivity" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
在Manifest中配置在你的启动Activity下面。当然不是所有android 7.0手机都支持shortcuts的。其实Android7.0就已经支持shortcuts,只是很多手机的系统桌面并不支持shortcuts,比如小米的 桌面就不支持。可以下载一些第三方桌面。
<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/shotrcut"/>
</activity>
动态的创建一个shortcuts
shortcuts的动态创建也很简单。有完整的api支持
//界面上只有两个按钮,一个 tv_save,一个tv_forbid。
if(v.getId()==R.id.tv_save){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
if(shortcutManager==null){
return;
}
List<ShortcutInfo> dynaShortcuts = new ArrayList<>();
ShortcutInfo.Builder builder = new ShortcutInfo.Builder(this,"mainActivity");
builder.setIcon(Icon.createWithResource(this,R.drawable.icon_main)); //设置图标
// builder.setActivity(new ComponentName(getPackageName(),MainActivity.class.getName()));
Intent intent = new Intent("android.intent.action.VIEW");
//这里的intent可以正常带一些参数。就像activity跳转一样,正常设置一些参数就好。
intent.setClassName(getPackageName(),MainActivity.class.getName()); //意图中配置包名和类名
builder.setIntent(intent); //设置intent
builder.setShortLabel("打开MAINActivity"); //设置短标题
builder.setLongLabel("进入到当前app的MAINActivity中"); //设置长标题
builder.setCategories(new TreeSet<String>(){{this.add("android.shortcut.conversation");}});
dynaShortcuts.add(builder.build());
shortcutManager.addDynamicShortcuts(dynaShortcuts); //添加动态的shortcuts
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); //这里能够保存成功,不表示,一定能够生效,shortcut需要桌面支持
//还有一些方法:比如
//shortcutManager.getMaxShortcutCountPerActivity(); //获取静态和动态shortcuts的最大个数
}else{
Toast.makeText(this,"当前系统不支持shortCuts",Toast.LENGTH_SHORT).show();
}
}else if(v.getId()==R.id.tv_forbid){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
if(shortcutManager!=null){
shortcutManager.disableShortcuts(new ArrayList<String>(){{this.add("mainActivity");}}); //禁用id为mainActivity的shortcuts。
Toast.makeText(this,"禁用成功...",Toast.LENGTH_SHORT).show();
}
}
}
}
大致,代码都在这里了,就不上传git了。
更多推荐
android shortcuts (android 7.1的快捷启动方式)
发布评论