Java 安装时在主屏幕上创建应用程序快捷方式,并打开其最后一个屏幕

Java 安装时在主屏幕上创建应用程序快捷方式,并打开其最后一个屏幕,java,android,shortcut,Java,Android,Shortcut,我正在尝试在安装应用程序时创建快捷方式。我正在使用以下代码 Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcutintent.putExtra("duplicate", false); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.iconname));

我正在尝试在安装应用程序时创建快捷方式。我正在使用以下代码

Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.iconname));
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.appicon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), DashActivity.class));
    sendBroadcast(shortcutintent);enter code here

但在这种情况下,它总是开放的。假设我正在使用应用程序,并且应用程序处于c活动中,并且它现在正在bacgkground中运行。当我单击快捷方式时,它正在打开DashActivity,而不是打开active ie c的窗口。正确的方法是从主屏幕侦听快捷方式请求——在清单中使用类似的意图过滤器:

<activity android:name=".ShortCutActivity" android:label="@string/shortcut_label">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

它正在打开DashActivity,因为在这一行中,它是用来打开这一行的。 putExtra(Intent.EXTRA\u SHORTCUT\u Intent,new Intent(getApplicationContext(),DashActivity.class))

使用您自己要打开的活动更改此活动的名称。
希望这能有所帮助。

来自
shortcutient.putExtra(Intent.EXTRA\u SHORTCUT\u Intent,new Intent(getApplicationContext(),DashActivity.class))

删除
DashActivity.class
并提供您的应用程序启动器活动名称…以便它能按您的预期工作。

DashActivity是启动器活动,android:launchMode=“singleTask”从活动中删除启动模式…它能工作,但我不能以其他方式工作?您所说的“我不能以其他方式”是什么意思对不起,我的意思是我不能删除模式,因为它用于不同的目的。有其他选择吗?我希望它应该打开最后一个actget current activity(获取当前活动),方法如下代码。Activity currentActivity=((MyApp)context.getApplicationContext()).getCurrentActivity():然后传递此当前活动而不是DashActivity。希望这能解决你的问题
// create shortcut if requested
ShortcutIconResource icon =
    Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

Intent intent = new Intent();

Intent launchIntent = new Intent(this,ActivityToLaunch.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

setResult(RESULT_OK, intent);