Android Launcher開發(四)自動添加案頭捷徑及實際開發中常見問題的解決方案

來源:互聯網
上載者:User

最近做到的應用做剛好需要添加捷徑的功能, 在參考了原始碼和網上一些其他資料後做了出來.

在做的時候遇到兩個問題,

一.  程式卸載後案頭捷徑仍然存在:

  關於此問題, 網上的資料和實際中很多應用程式的老版本或者目前的版本仍存在. 參考原始碼後,我找出瞭解決方案: 建立shortcut時需要設定 Extre_ShortCut_Intent 的action.和category,使建立的shortcut與自己的應用產生綁定的關係:

二. 需要判斷捷徑是否存在,捷徑的資訊,在系統應用launcher中以contentprovider的形式提供資料.

需要從com.android.launcher的launcher.db的favorites表中讀取捷徑的資訊

     對於lancher 的provider節點中的AUTHORITY屬性,在 Android屬性中變成了"com.android.launcher2.settings". 而2.2之前的版本為"com.android.launcher.settings"

 如果應用程式需要相容2.2以下的機器,需要擷取當前手機系統的版本號碼,更改Uri中的AUTHORITY.

解決了如上兩個問題,該功能就完善了,需要的朋友可以直接加到要上線的應用中.

效果;

全部代碼如下,已經加入了詳細的註解:

1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.autoLancher"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>      <!-- 捷徑資訊需要從setting中讀取 -->    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>     <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".LauncherDemo2Activity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            
              <!-如果想讓應用出現在長按Menu鍵選擇添加捷徑中,則需要加入以下代碼->             <intent-filter>

<action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest>

2. LauncherDemo2Activity:

package com.android.autoLancher;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.ContentResolver;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;public class LauncherDemo2Activity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                        boolean  flag =isShortcutInstalled();//如果已經建立,則不需要在建立          if(flag==false){                          AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);            builder.setTitle("是否為此應用建立案頭捷徑");            builder.setPositiveButton("是", new OnClickListener() {                                public void onClick(DialogInterface dialog, int which) {                    addShortcutToDesktop();                }            });                        builder.setNegativeButton("否", new OnClickListener() {                                public void onClick(DialogInterface dialog, int which) {                                    }            });                        builder.create().show();                                }              }    private void addShortcutToDesktop() {        Intent shortcut = new Intent(                "com.android.launcher.action.INSTALL_SHORTCUT");        // 不允許重建        shortcut.putExtra("duplicate", false);        // 設定名字        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");        // 設定表徵圖        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,                Intent.ShortcutIconResource.fromContext(this,                R.drawable.cat));        // 設定意圖和捷徑關聯程式        Intent intent = new Intent(this, this.getClass());        // 案頭表徵圖和應用綁定,卸載應用後系統會同時自動刪除表徵圖        intent.setAction("android.intent.action.MAIN");        intent.addCategory("android.intent.category.LAUNCHER");                shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);        // 發送廣播        sendBroadcast(shortcut);            }    /**     * 捷徑資訊是儲存在com.android.launcher的launcher.db的favorites表中     *      * @return     */    public boolean isShortcutInstalled() {        boolean isInstallShortcut = false;        final ContentResolver cr = LauncherDemo2Activity.this                .getContentResolver();       // 2.2系統是”com.android.launcher2.settings”,網上見其他的為"com.android.launcher.settings"        String AUTHORITY = null;        /*         * 根據版本號碼設定Uri的AUTHORITY         */        if(getSystemVersion()>=8){            AUTHORITY = "com.android.launcher2.settings";        }else{            AUTHORITY = "com.android.launcher.settings";        }                Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY                + "/favorites?notify=true");        Cursor c = cr.query(CONTENT_URI,                new String[] { "title", "iconResource" }, "title=?",                new String[] { getString(R.string.app_name) }, null);// XXX表示應用程式名稱。        if (c != null && c.getCount() > 0) {            isInstallShortcut = true;            System.out.println("已建立");        }        return isInstallShortcut;    }         /**     * 擷取系統的SDK版本號碼     * @return     */    private int getSystemVersion(){        return Build.VERSION.SDK_INT;    } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.