【发布时间】:2018-11-22 03:18:55
【问题描述】:
我使用主活动中的代码在主屏幕上创建了 android 应用快捷方式
快捷方式创建成功,我可以启动应用程序。
功能也可以正常工作,但是当我单击快捷方式时,会出现找不到应用程序的错误。
请检查我的代码并指导我如何解决这个问题。
这是我的主要活动代码:-
Context context = MainActivity.this;
SharedPreferences sharedPreferences;
boolean isAppInstalled = false;
这是我的 onCreate 代码
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled= sharedPreferences.getBoolean("isAppInstalled", false);
if(isAppInstalled==false){
Intent intent1 = new Intent(getApplicationContext(),MainActivity.class);
intent1.setAction(Intent.ACTION_MAIN);
Intent intent2 = new Intent();
intent2.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent1);
intent2.putExtra(Intent.EXTRA_SHORTCUT_NAME,"My Application");
intent2.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
intent2.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent2);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isAppInstalled",true);
editor.commit();
}
【问题讨论】: