在第一个屏幕的onCreate()方法中调用此方法.还要确保像我一样使用SharedPreferences检查应用是否第一次运行:
private void addShortcut() {
//Adding shortcut for MainActivity on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, this.getResources().getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
// TO check app is installed first time.
SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE);
if(!prefs.getBoolean("isFirstTime", false)){
addShortcut();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("isFirstTime", true);
editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12803 次 |
| 最近记录: |