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