閱讀941 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android--創建快捷方式和判斷是否已經創建

一般android應用程序安裝完成後是不會自動創建快捷方式的,所以可以自己在程序啟動時實現。

 需要權限  <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

和要點擊快捷方式對應的那個activity的屬性。

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

如何判斷快捷方式是否已經創建的方法,因為快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查詢此表得到,

需要權限 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> 

下麵給個例子分享給各位:

[java] view plaincopy
  1. import android.app.Activity;  
  2. import android.content.ContentResolver;  
  3. import android.content.Intent;  
  4. import android.content.Intent.ShortcutIconResource;  
  5. import android.database.Cursor;  
  6. import android.graphics.BitmapFactory;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. public class AddShortCutActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.             boolean  flag =IfaddShortCut();//如果已經創建,則不需要在創建  
  16.             if(flag==false){  
  17.                 addShortCut();  
  18.             }  
  19.     }  
  20.     public void addShortCut(){  
  21.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  22.         // 設置屬性  
  23.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));  
  24.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this, R.drawable.icon);  
  25.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);  
  26.           
  27.         // 是否允許重複創建  
  28.         shortcut.putExtra("duplicate"false);   
  29.         Intent intent = new Intent(Intent.ACTION_MAIN);  
  30.         intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
  31.         intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);  
  32.         intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  33.         intent.setClass(AddShortCutActivity.this, AddShortCutActivity.class);  
  34.         // 設置啟動程序  
  35.         System.out.println("createIcon");  
  36.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  37.         AddShortCutActivity.this.sendBroadcast(shortcut);  
  38.     }  
  39.    public  boolean  IfaddShortCut(){  
  40.       boolean isInstallShortcut = false ;    
  41.         final ContentResolver cr = AddShortCutActivity.this.getContentResolver();    
  42.         //本人的2.2係統是”com.android.launcher2.settings”,網上見其他的為"com.android.launcher.settings"  
  43.         final String AUTHORITY = "com.android.launcher2.settings";    
  44.         final Uri CONTENT_URI = Uri.parse("content://" +    
  45.                          AUTHORITY + "/favorites?notify=true");  
  46.         Cursor c = cr.query(CONTENT_URI,    
  47.         new String[] {"title","iconResource" },    
  48.         "title=?",    
  49.         new String[] {getString(R.string.app_name ) }, null);//XXX表示應用名稱。    
  50.                 if(c!=null && c.getCount()>0){    
  51.             isInstallShortcut = true ;    
  52.             System.out.println("已創建");  
  53.         }    
  54.         return isInstallShortcut ;    
  55.     }  
  56. }  


[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="https://schemas.android.com/apk/res/android"  
  3.       package="com.shao.add"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".AddShortCutActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.               <intent-filter>  
  16.                 <action android:name="android.intent.action.CREATE_SHORTCUT"></action>  
  17.             </intent-filter>  
  18.         </activity>  
  19.   
  20.     </application>  
  21.      <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>   
  22.       <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  23. </manifest>  


最後更新:2017-04-02 22:16:22

  上一篇:go jQuery中find和filter的區別
  下一篇:go ASP.NET4.0對服務器控件的ID的控製(節選自周公的博客)