天天看點

總結:android 建立快捷方式的兩種方式+判斷是否已經建立+删除快捷方式1.   在清單檔案裡面進行注冊:例如:2.  手動建立快捷方式3.判斷是否已經建立了快捷方式(在某些機型中需要判斷)4.删除5.聲明權限

1.   在清單檔案裡面進行注冊:例如:

<activity
            android:name="com.android.master.legend.widget.CreateSystemSettingsWidgetActivity"
            android:exported="true"
            android:icon="@drawable/ic_switcher_shortcut"
            android:label="@string/system_switcher_shortcut"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>      

這樣,就會自動加入到 系統launcher的快捷方式裡面 

2.  手動建立快捷方式

public static void createSystemSwitcherShortCut(Context context) {
        final Intent addIntent = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");
        final Parcelable icon = Intent.ShortcutIconResource.fromContext(
                context, R.drawable.ic_switcher_shortcut); // 擷取快捷鍵的圖示
        addIntent.putExtra("duplicate", false);
        final Intent myIntent = new Intent(context,
                SystemSwitcherActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                context.getString(R.string.switch_widget));// 快捷方式的标題
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的圖示
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的動作
        context.sendBroadcast(addIntent);
    }      

更具有通用性的寫法如下:

/** 
     * 為程式建立桌面快捷方式 
     */ 
    private void addShortcut(){  
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
               
        //快捷方式的名稱  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
        shortcut.putExtra("duplicate", false); //不允許重複建立  
 
        /****************************此方法已失效*************************/
        //ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName());  
        //shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));    
     /******************************end*******************************/
     Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
     shortcutIntent.setClassName(this, this.getClass().getName());
     shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
 
        //快捷方式的圖示  
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);  
               
        sendBroadcast(shortcut);  
    }       

小結:一般做法是在設定裡面加上手動建立快捷方式的設定。

            在程式第一次啟動的時候,手動建立一次快捷方式。

3.判斷是否已經建立了快捷方式(在某些機型中需要判斷)

private boolean hasShortcut()
{
        boolean isInstallShortcut = false;
        final ContentResolver cr = activity.getContentResolver();
        final String AUTHORITY ="com.android.launcher.settings";
        final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI,new String[] {"title","iconResource" },"title=?",
        new String[] {mapViewActivity.getString(R.string.app_name).trim()}, null);
        if(c!=null && c.getCount()>0){
            isInstallShortcut = true ;
        }
        return isInstallShortcut ;      

4.删除

/** 
     * 删除程式的快捷方式 
     */ 
    private void delShortcut(){  
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  
               
        //快捷方式的名稱  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));  
        String appClass = this.getPackageName() + "." +this.getLocalClassName();  
        ComponentName comp = new ComponentName(this.getPackageName(), appClass);  
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));  
               
        sendBroadcast(shortcut);  
               
    }        

5.聲明權限

在AndroidManifest.xml 檔案中聲明 建立和删除快捷方式時聲明權限

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

6.一個應用開啟多個的問題

現在都很流行在桌面上為自己的應用建立快捷方式,網上也很多例子,但是基本都是同一個,建立快捷方式的手法是對的,但是通過快捷方式開啟自己的應用的時候會發現程式菜單裡頭打開的應用和桌面快捷方式打開的應用竟然不是同一個,這樣會導緻一個應用開啟了多個。

一開始從activity的加載模式入手,把預設的activity的加載模式設定成了android:launchMode="singleInstance"   雖然每次都隻開啟了同一個頁面,但是每次都是從新加載的,而不是直接調到已經開啟的activity,這個互動很不理想;

經過仔細研究log發現,通過桌面快捷方式開啟應用和通過程式菜單開啟應用的日志資訊差了一個東西cat=[android.intent.category.LAUNCHER],通過快捷方式開啟應用并沒有列印出這個屬性,是以就嘗試在快捷方式建立的時候給他的intent添加一個屬性,  

ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());  
  Intent intent = new Intent(Intent.ACTION_MAIN).setComponent(comp);
  intent.addCategory(Intent.CATEGORY_LAUNCHER);
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);      

其中,關鍵的就是intent.addCategory(Intent.CATEGORY_LAUNCHER);  完美解決了标題提到的問題!

繼續閱讀