Androidアプリで、初回起動時にショートカットを生成するためのコード。
初回起動チェックをしないと起動毎にショートカットを生成してしまうため、注意すること。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> </intent-filter>
Context mContext=LaunchActivity.this; SharedPreferences appPreferences; boolean isAppInstalled = false; /** * check if application is running first time, only then create shorcut */ appPreferences = PreferenceManager.getDefaultSharedPreferences(this); isAppInstalled = appPreferences.getBoolean("isAppInstalled", false); if (isAppInstalled == false) { /** * create short code */ Intent shortcutIntent = new Intent(getApplicationContext(), LaunchActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "app_title"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent); /** * Make preference true */ SharedPreferences.Editor editor = appPreferences.edit(); editor.putBoolean("isAppInstalled", true); editor.commit(); }