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

Android 由程式主動建立桌面捷徑:Broadcast 實作

編輯:關於 Android 程式設計

桌面捷徑是 Launcher 最基本的元件,作用是直接啟動某個應用中的特定元件。一般使用者在應用清單上長按圖示即可建立,但除了這條路,開發者也能在程式中主動為使用者建立捷徑。

常見有兩種做法:

  1. 建立一個 Intent,以廣播形式通知 Launcher 建立捷徑。
  2. 為應用元件註冊符合條件的 IntentFilter,讓使用者在 Launcher 桌面手動新增(即上一篇介紹的方式)。

本文示範第一種,也就是完全由程式主導。

1. 版面配置(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/createShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20px"
        android:text="創建快捷鍵"/>

    <Button
        android:id="@+id/exit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20px"
        android:text="退出"/>
</LinearLayout>

2. 清單檔案

這裡的關鍵是 INSTALL_SHORTCUT 權限,缺少它就無法成功建立捷徑。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.ljq.action"
    android:versionCode="1"
    android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ShortCutAction" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="7" />

    <!-- 建立捷徑的權限 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
</manifest>

3. ShortCutAction 類別

package com.ljq.action;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ShortCutAction extends Activity implements OnClickListener {
    private Button createShortcut = null;
    private Button exit = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        createShortcut = (Button) findViewById(R.id.createShortcut);
        exit = (Button) findViewById(R.id.exit);
        createShortcut.setOnClickListener(this);
        exit.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.createShortcut:
                Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

                // 準備捷徑圖示
                Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.png);
                // 捷徑點擊後要開啟的目標
                Intent myIntent = new Intent(this, ShortCutAction.class);

                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);

                sendBroadcast(addIntent);   // 送出廣播
                break;

            case R.id.exit:
                System.exit(0);
                break;
        }
    }
}

4. 執行結果

三個 extras 的意義

  • EXTRA_SHORTCUT_NAME:顯示在桌面上的標題文字。
  • EXTRA_SHORTCUT_ICON_RESOURCE:捷徑圖示,透過 ShortcutIconResource.fromContext() 由 drawable 資源產生,也可改用 EXTRA_SHORTCUT_ICON 傳入 Bitmap。
  • EXTRA_SHORTCUT_INTENT:點擊捷徑時要啟動的 Intent,這裡指向自己的 Activity。

補充一點:部分 Launcher(尤其是各家客製化 ROM)對 INSTALL_SHORTCUT 的處理方式不一,可能不會真的建立,也不會回傳失敗訊息。因此若功能成敗很關鍵,建議在廣播後搭配 sendBroadcast(intent, receiver) 的形式接收 Launcher 的回覆廣播,據此提示使用者。另外 System.exit(0) 並非 Android 建議的結束方式,實務上改用 finish() 更合適。

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