一般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"/>
下面給個例子分享給各位:
</p><p>import android.app.Activity;<br />import android.content.ContentResolver;<br />import android.content.Intent;<br />import android.content.Intent.ShortcutIconResource;<br />import android.database.Cursor;<br />import android.graphics.BitmapFactory;<br />import android.net.Uri;<br />import android.os.Bundle;<br />public class AddShortCutActivity extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br />boolean flag =IfaddShortCut();//如果已經建立,則不需要在建立<br />if(flag==false){<br />addShortCut();<br />}<br /> }<br /> public void addShortCut(){<br /> Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");<br />// 設定屬性<br />shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));<br />ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this, R.drawable.icon);<br />shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);</p><p>// 是否允許重複建立<br />shortcut.putExtra("duplicate", false);<br />Intent intent = new Intent(Intent.ACTION_MAIN);<br />intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);<br />intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);<br /> intent.addCategory(Intent.CATEGORY_LAUNCHER);<br /> intent.setClass(AddShortCutActivity.this, AddShortCutActivity.class);<br />// 設定啟動程式<br /> System.out.println("createIcon");<br />shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);<br />AddShortCutActivity.this.sendBroadcast(shortcut);<br />}<br /> public boolean IfaddShortCut(){<br /> boolean isInstallShortcut = false ;<br /> final ContentResolver cr = AddShortCutActivity.this.getContentResolver();<br /> //本人的2.2系統是”com.android.launcher2.settings”,網上見其他的為"com.android.launcher.settings"<br /> final String AUTHORITY = "com.android.launcher2.settings";<br /> final Uri CONTENT_URI = Uri.parse("content://" +<br /> AUTHORITY + "/favorites?notify=true");<br /> Cursor c = cr.query(CONTENT_URI,<br /> new String[] {"title","iconResource" },<br /> "title=?",<br /> new String[] {getString(R.string.app_name ) }, null);//XXX表示應用程式名稱。<br /> if(c!=null && c.getCount()>0){<br /> isInstallShortcut = true ;<br /> System.out.println("已建立");<br /> }<br /> return isInstallShortcut ;<br /> }<br />}
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.shao.add"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <uses-sdk android:minSdkVersion="8" /></p><p> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".AddShortCutActivity"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> <intent-filter><br /> <action android:name="android.intent.action.CREATE_SHORTCUT"></action><br /> </intent-filter><br /> </activity></p><p> </application><br /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/><br /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/><br /></manifest>