我对Android开发非常陌生。
我已经用Java创建了一个应用程序,该应用程序可以接收额外的数据,并仅基于这些额外的数据显示一个对话框。因此,例如,多余的数据可能是msg =“ This is the message”。在这种情况下,对话框将显示“这是消息”。
该应用是使用以下命令远程执行的:
am start -a android.intent.action.VIEW -e msg "This is the message" -n zipper.notify/.MainActivity
但是,现在我需要能够处理在实例已经运行的情况下再次执行应用程序的情况。与其“关闭”正在运行的实例并启动该应用程序的新实例,我希望仅允许应用程序的一个实例运行,而当启动另一个实例时,只需让当前实例获取额外的数据并更新对话框中显示的文本。
这样,可以在不关闭应用程序然后重新启动的情况下更新消息。
如何实现?
我已了解有关在活动中设置launchMode
的信息。如果我将launchMode
设置为singleInstance
或singleTask
,当前运行的活动是否得到某种消息,我可以通过一种方法来处理该消息,以“捕获”第二次调用尝试并阅读更多内容数据?
以下工作吗?
singleTop
getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
处理onNewIntent()
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Log.d( "AGENT", "NEW INTENT Extra data = " + intent.getStringExtra("msg"));
}
答案 0 :(得分:0)
以下内容似乎运作良好。如果有人要补充,我将不胜感激。
launchmode
设置为singleTop
在onCreate()
方法中向活动添加以下内容
getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
处理onNewIntent()
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Log.d( "AGENT", "NEW INTENT Extra data = " + intent.getStringExtra("msg"));
}