Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 為應用程序創建桌面快捷方式技巧分享

android 為應用程序創建桌面快捷方式技巧分享

編輯:關於Android編程

我們開發一款軟件後,如果手機裝的軟件過多,去翻的話會很難翻的,所以,在主頁面有一個快捷方式的話會很不錯的,下面是詳細代碼:
復制代碼 代碼如下:
/**
* 創建桌面快捷方式
*/
private void createShortcut() {
SharedPreferences setting = getSharedPreferences("silent.preferences", 0);
// 判斷是否第一次啟動應用程序(默認為true)
boolean firstStart = setting.getBoolean("FIRST_START", true);
// 第一次啟動時創建桌面快捷方式
if (firstStart) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name2));
// 不允許重復創建
shortcut.putExtra("duplicate", false);
// 指定快捷方式的啟動對象
ComponentName comp = new ComponentName(this.getPackageName(), "." + this.getLocalClassName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
// 快捷方式的圖標
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.zhangxy);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
// 發出廣播
sendBroadcast(shortcut);
// 將第一次啟動的標識設置為false
Editor editor = setting.edit();
editor.putBoolean("FIRST_START", false);
// 提交設置
editor.commit();
}
}

然後在onCreate()方法裡加上上面方法名稱就行了:
復制代碼 代碼如下:
// 安裝後第一次啟動時創建桌面快捷方式
createShortcut();

最後在AndroidManifest.xml裡加上創建快捷方式的權限就行了:
復制代碼 代碼如下:
<!-- 創建桌面快捷方式的權限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved