AndroidShortCuts

先附屬一波效果圖吧

長按手機(jī)應(yīng)用彈出

點擊按鈕跳轉(zhuǎn)界面

靜態(tài)創(chuàng)建界面

第一步
進(jìn)入AndroidManifest中找到 然后再Activity中加入
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

<application
        .........
        <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>

第二步 在@xml/shortcuts中寫
我們直接運(yùn)行是不可以的,這里我們需要將這個文件生成v25目錄下的 Alt+Enter代碼提示會幫我們生成

生成v25包

  • android:icon="@drawable/add" ---> 顯示圖
  • 三個String類型需要自己到String文件下聲明
  • android:data ---> 顯示傳過去的值
  • shortcutShortLabel ---> 顯示默認(rèn)文字
  • android:targetClass / android:targetPackage 都需要換成自己的包名類名
  • android:targetClass ---> 選擇跳轉(zhuǎn)到哪個界面
    具體參數(shù)配置說明如下:


    參數(shù)配置
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/add"
        android:shortcutDisabledMessage="@string/static_disabled_message" 
        android:shortcutId="staticId"
        android:shortcutLongLabel="@string/static_shortcut_long_label"
        android:shortcutShortLabel="@string/static_shortcut_short_label">
        <intent
            android:action="android.intent.action.VIEW"
            android:data="content://baidu.com/祁威皓跳轉(zhuǎn)Come On"
            android:targetClass="com.baidu.androidshortcuts.TestActivity"
            android:targetPackage="com.baidu.androidshortcuts" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

第三布 在新建的Activity中獲取傳輸過來的值

if (getIntent().getData() != null && TextUtils.equals(getIntent().getAction(), Intent.ACTION_VIEW)) {
            Uri uri = getIntent().getData();
            List<String> pathSegments = uri.getPathSegments();
            if (pathSegments != null && pathSegments.size() > 0) {
                tv.setText(pathSegments.get(0));
            }
}

動態(tài)創(chuàng)建界面

動態(tài)快捷方式提供向指向應(yīng)用內(nèi)特定的跳轉(zhuǎn)或數(shù)據(jù)傳遞,這些跳轉(zhuǎn)和數(shù)據(jù)可能會在應(yīng)用執(zhí)行中發(fā)生變化。
此時需要借助 ShortcutManager 提供的 API 來完成動態(tài)快捷方式的相應(yīng)操作

  • 創(chuàng)建: 使用 setDynamicShortcuts() 重新定義動態(tài)快捷方式的完整列表
  • 添加: 使用 addDynamicShortcut() 來擴(kuò)充現(xiàn)有的動態(tài)快捷方式列表
  • 更新: 使用 updateShortcuts() 方法進(jìn)行更新現(xiàn)有的動態(tài)快捷方式列表
  • 刪除: 使用 removeDynamicShortcuts() 移除一組動態(tài)快捷方式,或者使用 removeAllDynamicShortcuts() 移除所有動態(tài)快捷方式

以創(chuàng)建為例,其他差不多,自行嘗試,具體的操作可參考下面的代碼

1. 創(chuàng)建 ShortcutInfo 對象

@TargetApi(Build.VERSION_CODES.N_MR1)
   private ShortcutInfo createShortcutInfo1() {
     return new ShortcutInfo.Builder(this, ID_DYNAMIC_1)
       .setShortLabel(getString(R.string.dynamic_shortcut_short_label1))
       .setLongLabel(getString(R.string.dynamic_shortcut_long_label1))
       .setIcon(Icon.createWithResource(this, R.drawable.add))
       .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://xiaweizi.cn/")))
       .build();
} 

**2. 調(diào)用 setDynamicShortcuts() 覆蓋掉之前的,重新設(shè)置新的動態(tài)快捷方式列表 **

private void setDynamicShortcuts() {
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
   ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
   List<ShortcutInfo> shortcutInfo = new ArrayList<>();
   shortcutInfo.add(createShortcutInfo1());
   shortcutInfo.add(createShortcutInfo2());
   if (shortcutManager != null) {
     shortcutManager.setDynamicShortcuts(shortcutInfo);
   }
 }
}

** 3. 可以配置 label 的字體顏色**

@TargetApi(Build.VERSION_CODES.N_MR1)
private ShortcutInfo createShortcutInfo2() {
 Intent intent = new Intent(this, TestActivity.class);
 intent.setAction(Intent.ACTION_VIEW);
 intent.putExtra("key", "fromDynamicShortcut");
 ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.BLUE);
 String label = getResources().getString(R.string.dynamic_shortcut_short_label2);
 SpannableStringBuilder colouredLabel = new SpannableStringBuilder(label);
 colouredLabel.setSpan(colorSpan, 0, label.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
 return new ShortcutInfo.Builder(this, ID_DYNAMIC_2)
   .setShortLabel(colouredLabel)
   .setLongLabel(getString(R.string.dynamic_shortcut_long_label2))
   .setIcon(Icon.createWithResource(this, R.drawable.link))
   .setIntent(intent)
   .build();
} 

-------------------------------持續(xù)更新------------------------------------------

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容