擁抱Android O,Android固定快捷方式App Shortcuts

由來

在新發布的Android 8.0功能和API中,Android 8.0 引入了對在應用啓動器圖標上顯示通知標誌的支持。通知標誌可反映某個應用是否存在與其關聯、並且用戶尚未予以清除也未對其採取行動的通知。通知標誌也稱爲通知點。簡而言之呢,就是在Android 8.0+加入了類似於IOS的3DTouch的功能。下面就是他的效果。


這個小工能的添加可謂是非常方便的,在我們日常的應用場景中,有時候我們可能想用QQ發一條空間動態。

    A[QQ] -->|常規方式| B[動態]
    B --> C[右上角+號按鈕]
    C --> D[發說說]
    A -->|App Shortcuts| E[快捷菜單]
    E --> D

App Shortcuts的增加,無疑簡化了用戶的操作,提升了用戶體驗度。

下面我們就用一個Demo來介紹下如何定製我們的App Shortcuts:


首先新建項目,找到AndroidManifest.xml在主Activity添加:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shortcutsdemo">

    <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>
            <!--這裏添加shortcuts配置文件-->
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts1" />
        </activity>

    </application>

</manifest>

然後在res/xml/下新建shortcuts1.xml文件:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/shortcut1"
        android:shortcutId="compose1"
        android:shortcutLongLabel="@string/shortcut1"
        android:shortcutShortLabel="@string/shortcut1">
        <intent
            android:action="android.intent.action.MAIN"
            android:data="快捷方式1"
            android:targetClass="com.example.shortcutsdemo.MainActivity"
            android:targetPackage="com.example.shortcutsdemo" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/shortcut2"
        android:shortcutId="compose2"
        android:shortcutLongLabel="@string/shortcut2"
        android:shortcutShortLabel="@string/shortcut2">
        <intent
            android:action="android.intent.action.MAIN"
            android:data="快捷方式2"
            android:targetClass="com.example.shortcutsdemo.MainActivity"
            android:targetPackage="com.example.shortcutsdemo" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher_round"
        android:shortcutDisabledMessage="@string/shortcut3"
        android:shortcutId="compose3"
        android:shortcutLongLabel="@string/shortcut3"
        android:shortcutShortLabel="@string/shortcut3">
        <intent
            android:action="android.intent.action.MAIN"
            android:data="快捷方式3"
            android:targetClass="com.example.shortcutsdemo.MainActivity"
            android:targetPackage="com.example.shortcutsdemo" />
        <intent
            android:action="android.intent.action.MAIN"
            android:data="快捷方式3"
            android:targetClass="com.example.shortcutsdemo.MainActivity"
            android:targetPackage="com.example.shortcutsdemo" />
        <categories android:name="android.shortcut.conversation" />
        <!-- 如果這裏的intent配置了多個的話,那麼點擊此項shortcut的話就會依次啓動,當點擊back的時候也是依次從棧頂退出頁面 -->
    </shortcut>
    <!-- 更多的shortcuts在這裏添加. -->
</shortcuts>

配置好了\res\xml\shortcuts1.xml文件後,這時候我們不用做其他的操作,直接運行就可以看到效果啦。


這時候當我們點擊某個item,就會跳轉到我們設置的對應的activity。

但這時候我們並無法區分它是用戶以別的頁面跳轉過來的還是通過點擊我們剛剛設置的shortcuts跳轉過來的,這時候怎麼辦呢,我們可以通過判斷當前頁面的intent來區分。

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView view = findViewById(R.id.text);
        view.setText(getIntent().getDataString());
}

這是我想到的一個方法,感覺應該還會有更好的方法。

以上就是靜態的添加shortcuts的方法,另外還有動態添加shortcuts的方法:

ShortcutManager shortcutManager = null;
ShortcutInfo shortcut = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        shortcutManager = getSystemService(ShortcutManager.class);
        shortcut = new ShortcutInfo.Builder(this, "id1")
                        .setShortLabel("Web site")
                        .setLongLabel("Open the web site")
                        .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                        .setIntent(new Intent(Intent.ACTION_VIEW,
                                        Uri.parse("https://www.mysite.example.com/")))
                        .build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}

使部分shortcut失效或激活shortcut:

shortcutManager.enableShortcuts(shortcutIds);
shortcutManager.disableShortcuts(shortcutIds);

效果:


參考地址:https://developer.android.google.cn/guide/topics/ui/shortcuts.html
原文地址:http://www.jianshu.com/p/569baa84ace8/
轉載請註明出處,未經允許不得轉載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章