Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

為 App 建立桌面捷徑:INSTALL_SHORTCUT 廣播三步驟

編輯:關於 Android 程式設計

想讓使用者在桌面(Launcher)上看到你的 App 捷徑,其實只要三步:

1. 建立一個 Intent,其 Action 為 com.android.launcher.action.INSTALL_SHORTCUT。
2. 透過該 Intent 的 Extra 屬性設定捷徑的名稱、圖示,以及點擊後要啟動的程式。
3. 呼叫 sendBroadcast() 送出廣播,捷徑就會出現在桌面。

下面用一個簡單範例演示。為了避免每次開啟 App 都重複建立捷徑,這裡用 SharedPreferences 記一個計數器:第一次安裝時建立捷徑,之後就不再建立。

Activity 實作

package com.home.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;

import com.example.testshortcut.R;

public class TestShortcutActivity extends Activity {

    private SharedPreferences sp;
    private Editor editor;
    private int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp = this.getSharedPreferences("testshortcut", MODE_PRIVATE);
        editor = sp.edit();

        // 讀取先前記錄的計數
        int readCount = sp.getInt("count", 0);
        if (readCount > 0) {
            Toast.makeText(this, "捷徑已存在,不必再建立", Toast.LENGTH_LONG).show();
            return;
        }

        // 建立「增加捷徑」的 Intent
        Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        // 捷徑名稱
        String title = "我的應用";

        // 載入捷徑圖示
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);

        // 點擊捷徑後要啟動的程式(這裡啟動自己)
        Intent myIntent = new Intent(this, TestShortcutActivity.class);

        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);

        // 送出廣播
        sendBroadcast(addIntent);

        // 記錄已建立過
        editor.putInt("count", count);
        editor.commit();
    }
}

權限宣告

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

Activity 的設定

除了 MAIN/LAUNCHER 這個標準的啟動入口之外,還要多加一個 CREATE_SHORTCUT 的 intent-filter,讓 Launcher 知道這個 App 允許被建立捷徑。

<activity
    android:name="com.home.activity.TestShortcutActivity"
    android:label="@string/app_name" >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- 定義可加入桌面 Launcher -->
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />
    </intent-filter>

</activity>

幾個實務提醒

用 SharedPreferences 判斷是否已建立並不可靠。使用者完全可以自己把捷徑刪掉,此時 App 內的計數仍是 1,就會永遠不再補建。若真的需要確保捷徑存在,比較好的做法是提供一個明確的「加入桌面」按鈕,讓使用者自行觸發。

各家的 Launcher 支援度不一。INSTALL_SHORTCUT 屬於非官方(未列入正式 API 文件)的約定,部分客製化 Launcher 會忽略這個廣播,因此務必搭配 try-catch 或至少不要依賴它作為唯一入口。

如果要發布到 Google Play,建議改用官方 API。Android 8.0(API 26)之後提供了 requestPinShortcut(),會由系統跳出標準的「加到主畫面」對話框,行為一致性與使用者告知都更好:

ShortcutManager sm = getSystemService(ShortcutManager.class);
if (sm.isRequestPinShortcutSupported()) {
    ShortcutInfo info = new ShortcutInfo.Builder(this, "my_shortcut")
            .setShortLabel("我的應用")
            .setIcon(Icon.createWithResource(this, R.drawable.ic_launcher))
            .setIntent(new Intent(this, TestShortcutActivity.class))
            .build();
    sm.requestPinShortcut(info, null);
}

相反的,若要移除自己建立的動態捷徑,可用 removeDynamicShortcuts();而 com.android.launcher.permission.UNINSTALL_SHORTCUT 這個權限實際上並不受系統保護,任何 App 都能移除捷徑,這一點在評估捷徑存在性時要特別注意。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved