Android:使用快捷方式启动应用程序,执行操作并退出?

时间:2013-01-23 01:44:50

标签: android android-activity shortcut

我有一个可以执行某些操作的Android应用程序。但是,我还允许在主屏幕上创建快捷方式来执行某些操作。您可以在应用程序中执行某项操作,方法是按下调用方法的按钮(让我们调用方法DoAction),我希望允许用户直接从快捷方式执行操作。单击快捷方式时,它会打开主活动并像按钮一样调用DoAction,然后调用活动上的finish()来关闭它。

但是,当应用程序已在RAM中打开(最小化)时会出现问题。在快捷方式创建的活动中调用finish()之后,应用程序的旧运行实例将被带到前面(我不希望发生这种情况)。

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

这是我用来创建快捷方式的工作代码:

Intent shortcutIntent = new Intent(ShortcutActivity.this, com.example.myproject.ClassToOpen.class);
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // This was the line that I needed to add

ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(ShortcutActivity.this, R.drawable.shortcut);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");
setResult(RESULT_OK, intent);
相关问题