android7.1使用Static Shortcuts(快捷方式)

来源:互联网 发布:mysql主从指定数据库 编辑:程序博客网 时间:2024/04/28 06:50

android7.1推出后:提供了BottomNavigationView,可以用于实现应用底部的Tab。
RecyclerView提供了默认的分割线的实现DividerItemDecoration,
可以通过android:listDivider进行设置,支持横或者纵。
这里是对Static Shortcuts的探究,和实现步骤:

效果如下:
这里写图片描述

第一步:Static Shortcuts是使用xml方式进行注册的,在res/xml目录下创建一个新的xml文件,命名为shortcuts.xml

<?xml version="1.0" encoding="utf-8"?><shortcuts xmlns:android="http://schemas.android.com/apk/res/android">    <shortcut        android:shortcutId="shortcut1"        android:enabled="true"        android:icon="@drawable/ic_launcher"        android:shortcutShortLabel="@string/zhujialan"        android:shortcutLongLabel="@string/zhujialan_long"        android:shortcutDisabledMessage="@string/zhujialan_disabled">        <intent            android:action="zhujialan"            android:targetPackage="cn.hnshangyu.shortcuts"            android:targetClass="cn.hnshangyu.shortcuts.MainActivity" />    </shortcut>    <shortcut        android:shortcutId="shortcut2"        android:enabled="false"        android:icon="@drawable/ic_launcher"        android:shortcutShortLabel="@string/huangxiaoguo"        android:shortcutLongLabel="@string/huangxiaoguo_long"        android:shortcutDisabledMessage="@string/huangxiaoguo_disabled">        <intent            android:action="huangxiaoguo"            android:targetPackage="cn.hnshangyu.shortcuts"            android:targetClass="cn.hnshangyu.shortcuts.MainActivity" />    </shortcut>    <shortcut        android:shortcutId="shortcut3"        android:enabled="true"        android:icon="@drawable/ic_launcher"        android:shortcutShortLabel="@string/hekang"        android:shortcutLongLabel="@string/hekang_long"        android:shortcutDisabledMessage="@string/hekang_disabled">        <intent            android:action="hekang"            android:targetPackage="cn.hnshangyu.shortcuts"            android:targetClass="cn.hnshangyu.shortcuts.MainActivity" />    </shortcut>    <shortcut        android:shortcutId="shortcut4"        android:enabled="true"        android:icon="@drawable/ic_launcher"        android:shortcutShortLabel="@string/wanghuiguo"        android:shortcutLongLabel="@string/wanghuiguo_long"        android:shortcutDisabledMessage="@string/wanghuiguo_disabled">        <intent            android:action="wanghuiguo"            android:targetPackage="cn.hnshangyu.shortcuts"            android:targetClass="cn.hnshangyu.shortcuts.MainActivity" />    </shortcut></shortcuts>在这里的几个属性的意思分别是:shortcutId, 不用多说, 这肯定是一个唯一的idenabled, 表示这个shortcut是否可用shortcutShortLabel, 这里是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示shortcutDisabledMessage, 这个配置是在我们选择一个不可用的shortcut时给用户的一个提示intent, 这里表示我们点击shortcut时要干嘛, targetPackage是指定一个目标应用的包名, targetClass是我们要跳转的目标类, 这里要注意的是android:action一定要配置, 否则会崩溃categories, 这个东西目前位置官方只给提供了android.shortcut.conversation

在这里如效果图,shortcut的顺序是从下向上排列的,第二个的enabled属性设置为false 不显示,其他的为true显示 前三个的shortcutLongLabel比较短,因此显示shortcutLongLabel内容,第四个shortcutLongLabel比较长,因此显示shortcutShortLabel内容。

显示内容如下:

<resources>    <string name="app_name">Shortcuts</string>    <string name="zhujialan">我是zhujialan</string>    <string name="zhujialan_long">zhujialan</string>    <string name="zhujialan_disabled">不可用</string>    <string name="huangxiaoguo">我是huangxiaoguo</string>    <string name="huangxiaoguo_long">huangxiaoguo</string>    <string name="huangxiaoguo_disabled">不可用</string>    <string name="hekang">我是hekang</string>    <string name="hekang_long">hekang</string>    <string name="hekang_disabled">hekang不可用</string>    <string name="wanghuiguo">我是wanghuiguo</string>    <string name="wanghuiguo_long">wanghuiguo_long_long_long_long</string>    <string name="wanghuiguo_disabled">4不可用</string></resources>

第二步:需要在manifest中配置activity的地方使用, 而且这个activity是有要求的.
能配置shortcuts的activity必须要有action是Android.intent.action.MAIN和category是android.intent.category.LAUNCHER! 不然没有效果的

这个配置如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.hnshangyu.shortcuts">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        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/shortcuts" />        </activity>    </application></manifest>

到这里shortcut就配置完成了,但是怎么和他进行交互呢,这就是个问题了,其实很简单。

第三步:shortcut与activity进行交互

package cn.hnshangyu.shortcuts;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private final static String ONE = "zhujialan";    private final static String TWO = "huangxiaoguo";    private final static String THREE = "hekang";    private final static String FOUR = "wanghuiguo";    private TextView textview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textview = (TextView) findViewById(R.id.textview);        switch (getIntent().getAction()) {            case ONE:                textview.setText(ONE);                break;            case TWO:                textview.setText(TWO);                break;            case THREE:                textview.setText(THREE);                break;            case FOUR:                textview.setText(FOUR);                break;            default:                break;        }    }}

我们长按桌面图标后,选择点开zhujialan其效果如下:

这里写图片描述

好了,到这里Static Shortcuts就结束了,有什么不妥之处,还请指教,谢谢~

0 0