1、在桌面增加Android應用快捷方式
public void createShortCut() {
// 創建快捷方式的Intent
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不允許重復創建
shortcutintent.putExtra("duplicate", false);
// 需要現實的名稱
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// 快捷圖片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 點擊快捷圖片,運行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
// 發送廣播。OK
sendBroadcast(shortcutintent);
}
在AndroidManifest.xml中增加權限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2、判斷快捷方式是否已經存在
public boolean hasShortcut(Activity activity, String shortcutName) {
String url = "";
url = "content://" + getAuthorityFromPermission(activity, "com.android.launcher.permission.READ_SETTINGS") + "/favorites?notify=true";
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name).trim() }, null);
if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
}
return false;
}
在AndroidManifest.xml中增加權限
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
因為不同的手機廠商可能對手機系統進行了修改使用原生的
“content://com.android.launcher.settings/favorites?notify=true"
或者
"content://com.android.launcher2.settings/favorites?notify=true"
並不能准確判斷 需要通過權限去獲取當前手機provider.authority
private 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;
}
Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm
Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm
Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm
64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm
Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11