創建快捷方式代碼:
public static final String READ_SETTINGS_PERMISSION = "com.Android.launcher.permission.READ_SETTINGS";
public static final String INSTALL_SHORTCUT_PERMISSION = "com.android.launcher.action.INSTALL_SHORTCUT";
private static final String TAG = "WowUtils";
/**
* 創建桌面快捷方式
* @param context
* @param 點擊快捷方式進入的Activity
* @param title 快捷方式顯示名
* @param iconRes 快捷方式圖標的resource id
*/
public static void createShortcut(Context context,Class activity,String title,int iconRes){
if(context == null || activity == null || isShortcutExist(context)){
return ;
}
Intent addIntent = new Intent(INSTALL_SHORTCUT_PERMISSION);
Parcelable icon = Intent.ShortcutIconResource.fromContext(context,iconRes);// 獲取快捷鍵的圖標
addIntent.putExtra("duplicate", false);
Intent myIntent = new Intent(context,activity);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,title);// 快捷方式的標題
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的圖標
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);// 快捷方式的動作
context.sendBroadcast(addIntent);
return ;
}
public static boolean isShortcutExist(Context context) {
String shortcutTitle = getShortcutTitle(context);
Log.i(TAG, "getShortcutTitle=" + shortcutTitle);
return !TextUtils.isEmpty(shortcutTitle);
}
public static String getShortcutTitle(Context context){
if(context == null){
return null;
}
String appLabel = null;
try {
PackageManager pm = context.getPackageManager();
appLabel = pm.getApplicationLabel(pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA)).toString();
} catch (Exception e) {
return null;
}
String authority = getAuthorityFromPermission(context,READ_SETTINGS_PERMISSION);
Log.i(TAG, "getAuthorityFromPermission=" + authority);
final String uriStr = "content://" + authority + "/favorites?notify=true";
final Uri uri = Uri.parse(uriStr);
final Cursor c = context.getContentResolver().query(uri,
new String[] { "title" }, "title=?", new String[] { appLabel },
null);
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do{
return c.getString(c.getColumnIndexOrThrow("title"));
}while(c.moveToNext());
}
return null;
}
/**
* The launcher is an Application under the Handset Manufacturer
* responsibility. The Authority is then not always
* "com.android.launcher2.settings". The Handset Manufacturer may rewrite
* its own. It can be "com.android.twlauncher" or anything else depending on
* the Java package. You need to retrieve the right authority by searching
* for a provider that declares the read/write permissions
* "com.android.launcher.permission.READ_SETTINGS" or
* "com.android.launcher.permission.WRITE_SETTINGS".
*
* @param context
* @param permission
* @return e.g. com.baidu.launcher2.settings
*/
public static String getAuthorityFromPermission(Context context, String permission){
if (permission == null) return null;
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission))
return provider.authority;
if (permission.equals(provider.writePermission))
return provider.authority;
}
}
}
}
return null;
}
Manifest.xml添加權限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
代碼使用示例:
WowUtils.createShortcut(this, MainActivity.class, getString(R.string.app_name), R.drawable.ic_launcher);
Android應用程序創建桌面快捷方式示例下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2014年資料/1月/18日/Android應用程序創建桌面快捷方式
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11