天天看點

(轉)Android建立桌面快捷方式兩種方法

【it168技術】android在桌面上生成快捷方式有兩種情況,一種是直接在桌面直接生成;一種是長按桌面,在彈出的快捷菜單中生成。

  談談在桌面上直接生成。個人覺得這個比較爽快,既然都是快捷方式了幹嘛還要再隐藏一層呢?當然喜歡桌面幹淨的就比較喜歡第二個了。

  第一個是通過廣播(broadcast)的形式向luncher發送請求生成快捷方式的。

  在網上找到關于這方面的注冊資訊。

<!--設定wallpapaer的activity -->

        <!-- intent received used to install shortcuts from other applications -->

        <receiver

            android:name="com.android.launcher2.installshortcutreceiver"

            android:permission="com.android.launcher.permission.install_shortcut">

            <intent-filter>

                <action android:name="com.android.launcher.action.install_shortcut" />

            </intent-filter>

        </receiver>

  可以看出,要在桌面上建立快捷方式就需要權限了:

  android:permission="com.android.launcher.permission.install_shortcut。

  是以在我們的manifest.xml檔案中,我們需要加入下面這段話:

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

  

  下面就是代碼層的實作:

  假如我在一個activity中建立一個建立快捷方式的方法:createshortcut();

public void createshortcut(){

//建立快捷方式的intent

                intent shortcutintent = new intent("com.android.launcher.action.install_shortcut");

                //不允許重複建立

                shortcutintent.putextra("duplicate", false);

                //需要現實的名稱

                shortcutintent.putextra(intent.extra_shortcut_name, getstring(r.string.shortcutname));

                //快捷圖檔

                parcelable icon = intent.shortcuticonresource.fromcontext(getapplicationcontext(), r.drawable.icon);

                shortcutintent.putextra(intent.extra_shortcut_icon_resource, icon);

                //點選快捷圖檔,運作的程式主入口

                shortcutintent.putextra(intent.extra_shortcut_intent, new intent(getapplicationcontext() , enteractivity.class));

                //發送廣播。ok

                sendbroadcast(shortcutintent);

}

  二、長按桌面彈出的桌面快捷方式建立

  第一頁談過直接在桌面生成快捷方式,現在說說如何在添加到一個shortcuts清單中,就是你長按桌面彈出來的那個東東。

  首先在注冊activity時,需要添加一個action為android.intent.action.create_shoertcut的intentfilter.如下所示:

<activity android:name="shortcuttest">

                <action android:name="android.intent.action.create_shortcut"/>

        </activity>

  接下來就是就是設定快捷方式的圖示、名稱、事件等屬性。這裡圖表的生成,android裡提供了專門的方法來生成。

public class shortcuttest extends activity{

    @override

    protected void oncreate(bundle savedinstancestate) {

        // todo auto-generated method stub

        super.oncreate(savedinstancestate);

    }

    public void createshortcut(){

        intent addshortcut;

        //判斷是否需要添加快捷方式

        if(getintent().getaction().equals(intent.action_create_shortcut)){

            addshortcut = new intent();

            //快捷方式的名稱

            addshortcut.putextra(intent.extra_shortcut_name , "我的快捷方式");

            //顯示的圖檔

            parcelable icon = shortcuticonresource.fromcontext(this, r.drawable.icon);

            addshortcut.putextra(intent.extra_shortcut_icon_resource, icon);

            //快捷方式激活的activity,需要執行的intent,自己定義

            addshortcut.putextra(intent.extra_shortcut_intent, new intent());

            //ok,生成

            setresult(result_ok, addshortcut);

        }else{

            //取消

            setresult(result_canceled);

        }