Android create pinned shortcut創(chuàng)建桌面快捷方式

前言

本文主要針對文章Create shortcuts中動態(tài)創(chuàng)建桌面快捷方式的解釋和例子。在8.0系統(tǒng)中,創(chuàng)建桌面快捷方式的廣播com.android.launcher.action.INSTALL_SHORTCUT不再生效,創(chuàng)建桌面快捷方式需要用另外的方法。由于文章中沒有詳細(xì)的例子而且表達不是很清楚,筆者也一頭霧水,經(jīng)過了多方的嘗試,最后才明白其中的意思,希望能給同樣遇到困惑的人一點幫助。轉(zhuǎn)載請注明來源「Bug總柴」

主動創(chuàng)建pinned shortcuts

主動創(chuàng)建pinned shortcuts的意思是可以通過代碼讓用戶選擇是否需要在桌面快捷方式。

/**
 * 這里用ShortcutManagerCompat是因為ShortcutManager的minsdkversion要求至少是25
 */
private void createShortCut() {
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
        ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, "id1")
                .setShortLabel("Website")
                .setLongLabel("Open the website")
                .setIcon(IconCompat.createWithResource(this, R.drawable.ic_logo_app))
                .setIntent(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.mysite.example.com/")))
                    .build();

        Intent pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, shortcut);
        PendingIntent successCallback = PendingIntent.getBroadcast(this, /* request code */ 0,
                pinnedShortcutCallbackIntent, /* flags */ 0);

        ShortcutManagerCompat.requestPinShortcut(this, shortcut, successCallback.getIntentSender());
    }
}

運行這段代碼后,會有這樣的提示給到用戶如下圖:


device-2018-11-23-151817.png

當(dāng)用戶點擊【添加】后會在桌面顯示快捷方式,不過現(xiàn)在的快捷方式都帶了一個下標(biāo)如下圖:


image.png

通過桌面小部件方式添加快捷方式

這種方式屬于用戶通過添加桌面小部件方式手動添加,我們需要創(chuàng)建一個activity來表明我們的應(yīng)用有這樣的快捷方式小部件,并且處理添加的行為。

// 在AndoidManifest文件中添加activity
<activity android:name=".activity.AddShortcutActivity">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

然后在Activity中創(chuàng)建快捷方式:

public class AddShortcutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_shortcut);
        Toast.makeText(this, "add shortcut", Toast.LENGTH_SHORT).show();
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
            ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, "id1")
                    .setShortLabel("Website")
                    .setLongLabel("Open the website")
                    .setIcon(IconCompat.createWithResource(this, R.drawable.ic_logo_app))
                    .setIntent(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://www.mysite.example.com/")))
                    .build();

            Intent pinnedShortcutCallbackIntent = ShortcutManagerCompat.createShortcutResultIntent(this, shortcut);
            setResult(RESULT_OK, pinnedShortcutCallbackIntent);
            finish();
        }
    }
}

再次運行代碼之后,可以在系統(tǒng)添加桌面小部件的地方看到有我們創(chuàng)建的小部件,這一個時候就可以拖動它到桌面了:)

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容