安卓7.1 新特性Shortcut

來源:互聯網
上載者:User

標籤:net   記憶體   .text   電腦   category   amp   create   時間   快捷   

介紹

Shortcut 是Google在API25提出來的 類似蘋果3D touch 但是沒有壓力感應.在安卓中完全就是長按.
來看下效果吧:

是不是很贊? 那麼請隨本文一起學習吧

更新
  1. 建立項目 在你項目下的build.gradle下

    以下目的很簡單更新你編譯工具 和指定項目版本

    1. compileSdkVersion 25
    2. buildToolsVersion “25.0.0”
    3. minSdkVersion 25
    4. targetSdkVersion 25
  2. 更新platform-tools 到25
    開啟SDK Manager

    如果你的Android SDK Platform-tools小於25那麼請勾選然後點右下角更新

靜態寫法

靜態寫法?說白了和BroadcastReceiver(廣播接受者)一樣 .一個在資訊清單檔中註冊廣播我們稱為靜態,用代碼註冊稱為動態

  1. 在res建立xml檔案夾

  2. 在res/xml下建立一個檔案命名為my_shortcut.xml字串貌似必須引用方法比如@string/xxxx
    具體內容

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">    <shortcut        android:shortcutId="settings"        android:enabled="true"        android:icon="@mipmap/ic_launcher"        android:shortcutShortLabel="@string/my_short"        android:shortcutLongLabel="@string/my_long"        android:shortcutDisabledMessage="@string/my_disable">        <intent            android:action="android.intent.action.VIEW"            android:targetPackage="com.example.administrator.myapplication"            android:targetClass="com.example.administrator.myapplication.MainActivity" />        <intent            android:action="android.intent.action.VIEW"            android:targetPackage="com.example.administrator.myapplication"            android:targetClass="com.example.administrator.myapplication.SettingsActivity" />        <categories android:name="android.shortcut.conversation"/>    </shortcut></shortcuts>

    參數說明

    shortcut 屬性說明:
    android:shortcutId 就是一個id標誌 後面動態註冊會講到
    android:enabled 是否可用 如果不可用那麼將不顯示此快捷
    android:shortcutShortLabel 快捷短名:大家注意到一開始的沒?快捷是可以脫出來在變成一個案頭捷徑表徵圖.那麼此表徵圖的名字就是這個
    android:shortcutLongLabel :快捷長名 長按標彈出來列表框中每個快捷名
    android:shortcutDisabledMessage: 當快捷不可用時使用者點擊會提示此文字 後面動態會詳細說明
    intent屬性說明:

    假設1:shortcut (看清楚不是shorcuts 沒有s哦)下只有一個intent 那麼結果:使用者點擊此快捷使用者跳轉到intent制定的activity

    假設2:shortcut 下有兩個intent 我們按照順序命名為intent1intent2 那麼使用者點擊快捷的時候將會跳轉到intent2 此時 若使用者按下back鍵(返回鍵) 那麼將會跳轉到intent1的介面

    categories 屬性說明
    反正就一個值就是上面寫的 寫死即可

  3. 在資訊清單檔註冊
    注意一個小坑:註冊資訊必須要在activity為啟動項的activity的根標籤註冊寫下<meta-data>

     <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/my_shortcut"/>        </activity>

    做法如下:
    下面少打錯了”android.app.shortcuts” 下面少打了個s (電腦實在太卡了,不想重錄)注意!!!!!!!!

  4. 效果展示:

  5. 小知識點
    假如:你開啟快捷item的程式所在的應用已經有多個activity在回退棧 請猜猜會怎麼樣?這裡留給讀者自行嘗試..哪怕什麼都沒有反生你也可以增加記憶嘛

動態寫法 -添加

特點和廣播接受者一樣靈活
核心代碼(本例只要點擊”建立”按鈕會執行下面方法產生快捷):

 //動態添加    public void onclick2(View view) {        mShortcutManager = getSystemService(ShortcutManager.class);        List<ShortcutInfo> infos = new ArrayList<>();        //快捷最多隻能有5個        // getMaxShortcutCountPerActivity只能返回5        for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {            Intent intent = new Intent(this, SettingsActivity.class);            intent.setAction(Intent.ACTION_VIEW);            Intent intent2 = new Intent();            intent2.setAction("fmy.fmy");            intent2.setClassName(getPackageName(),getPackageName()+".MainActivity.java");            Intent[] intents = new Intent[2];            //開始點擊快捷時跳進此 返回鍵跳入intent2 其他類似            intents[0]=intent;            intents[1]=intent2;                //第一個參數 上下文                //第二個參數id嘛                        ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)                    .setShortLabel("短的名字"+i+"")                    .setLongLabel("長的名字:" + i+"")                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//                   .setIntent(intent)                    .setIntents(intents)                    .build();            infos.add(info);        }        mShortcutManager.setDynamicShortcuts(infos);    }

效果:

解析:我一開始沒有點擊”建立”按鈕 直接在案頭長按按鈕發現沒有任何反應.然後進入程式按下建立按鈕並返回案頭發現可以長按點出快捷

注意和靜態寫法一起的坑(算本人經驗吧):

那些年我們一起踩過的坑—>>上面的代碼會動態建立5個快捷點擊item.但是如果你此時靜態寫一個了快捷item那麼恭喜你見紅了(出現異常)

Caused by: java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded

解決:先擷取其已有的快捷item數量然後要麼移除原來的,要麼減少你建立.或者更新 這就是我為什麼知道只能建立5個原因.解決方案讀者看完 “動態寫法-更新(覆蓋)”和”動態寫法-刪除”自然會明白,如果想先解決問題那麼請直接拷貝 更新 和 刪除 中部分核心代碼

動態寫法 -更新(覆蓋)

如果你想某些時候改變某些快捷item的名字或者意圖(intent)那麼請參照以下代碼

 public void onclick3(View view) {        Intent intent2 = new Intent();        intent2.setAction("fmy.fmy");        intent2.setClassName(getPackageName(),getPackageName()+".MainActivity.java");//設定id為id1 會覆蓋原來快捷item為id為id1的快捷//如果沒有則什麼都不會發生        ShortcutInfo info = new ShortcutInfo.Builder(this,"id1")                .setIntent(intent2)                .setLongLabel("動態更新的長名")                .setShortLabel("動態更新的短名")                .build();        mShortcutManager = getSystemService(ShortcutManager.class);        List<ShortcutInfo> dynamicShortcuts = mShortcutManager.getDynamicShortcuts();        mShortcutManager.updateShortcuts(Arrays.asList(info));    }

效果:

動態寫法 -刪除(不可用)

下面一小段描述轉載的(我不想寫,再說次作者寫這個描述非常不錯此段描述原作者地址)
我們先來介紹一個名詞-Pinning Shortcuts, 這是個啥玩意呢? 其實對於Shortcut, Android是允許我們直接放到案頭的, 這樣就更加方便了使用者的操作, google把他稱作為Pinning Shortcuts, 具體啥樣, 我們來張圖就明白了.

對於這個Pinning Shortcuts, google的文檔說, 我們開發人員是沒有權利去刪除的, 能刪除它的只有使用者. 那我該項功能刪除了咋辦? 這東西還在案頭上, 是不是APP要崩? 當然Google考慮到了這點, 它允許我們去disable這個shortcut. 讓其變為灰色 使用者點擊時提示個小土司
好了引用結束 感謝原作者

我們在案頭長按拖出來的快捷item到案頭 這個item對象為ShortcutInfo
代碼是最好的老師:

//刪除    public void onclick(View view) {        mShortcutManager = getSystemService(ShortcutManager.class);        //擷取所有被拉取出來的快捷item(如果一個item都沒有被拉出那麼返回長度為0)        List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();        //遍曆所有的被拉出的item 然後讓其變成灰色不可點擊        for (ShortcutInfo info :                infos ) {            //此時被拉出的快捷item 變為灰色 使用者再點擊 會彈出多士內容為第二個參數 "不可點擊哦"           // 此時案頭長按原程式表徵圖彈出的快捷列表已經沒有了            mShortcutManager.disableShortcuts(Arrays.asList(info.getId()), "不可點擊哦");            List<ShortcutInfo> dynamicShortcuts = mShortcutManager.getDynamicShortcuts();            Log.e("TAG","大小"+dynamicShortcuts.size());            //雖然不可見但是你 依然要移除在動態添加列表裡的東西 不過我調用disableShortcuts            // 後發現其大小變了.內部應該調用此方法了.由於電腦太卡沒下載源碼 所以保險起見寫上吧            mShortcutManager.removeDynamicShortcuts(Arrays.asList(info.getId()));        }        List<ShortcutInfo> dynamicShortcuts = mShortcutManager.getDynamicShortcuts();        Log.e("TAG","大小"+dynamicShortcuts.size());    }
小知識點
  1. 使用者刪除資料時 被拖出來快捷item會被刪除
  2. 使用者刪除資料時 動態建立的item 你在案頭在長按程式表徵圖也沒有 需要重新寫入
  3. 使用者卸載時被拖出來快捷item會被刪除
關於這篇博文

我偶然看到這個7.1新特性 於是一直在找學習資料.然後想寫下 期間看了幾篇文章 並結合自己體會寫下來.這篇部落格用到模擬器要用SDK manager 下載鏡像 因為只有它有7.1鏡像 genymotion 最新的也就只有7.0 而已.運行Google內建鏡像及其耗費記憶體 我就4G記憶體 開完stuio和部落格和模擬器 記憶體只剩下80mb 卡的程度可想而知.但是一直想寫一篇高品質的博文.於是硬著頭皮卡了5個小時寫下了.由於時間倉促錯漏在所難免,由於卡到不行不敢點擊源碼去看 而且我也沒下載.如果以上文字對你有那麼一點帶你協助 將是我最大的欣慰;

安卓7.1 新特性Shortcut

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.