当某个事件发生时,我的应用程序的线程会将消息发送给活动。应用程序处于活动状态时一切正常。但是如果应用程序被最小化,那么活动就无法从线程获得消息。如果线程中发生事件,如何将我的应用程序移到最前端?
我尝试使用BroadcastReceiver:
public class ReadCardReciver extends BroadcastReceiver {
Context mContext;
public static final String ACTION = "com.test.action.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
String action = intent.getAction();
if (action.equalsIgnoreCase(ACTION)) {
Intent activivtyIntent = new Intent(context, MainMenuActivity.class);
activivtyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activivtyIntent);
}
}
}
但如果我使用FLAG_ACTIVITY_NEW_TASK,则创建一个新活动,但我需要打开一个现有活动。如果我使用FLAG_ACTIVITY_REORDER_TO_FRONT,那么我有例外:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我只需将我的应用程序带到最前沿。我怎么能这样做?
ADD1 :我有一个例外,如果我设置了android:launchMode =“singleTop”:
08-08 17:44:25.350 668-668/com.test W/System.err: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-08 17:44:25.360 668-668/com.test W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:1123)
08-08 17:44:25.360 668-668/com.test W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:1110)
08-08 17:44:25.370 668-668/com.test W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
08-08 17:44:25.370 668-668/com.test W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
08-08 17:44:25.370 668-668/com.test W/System.err: at com.test.ReadCardReciver.onReceive(ReadCardReciver.java:23)
答案 0 :(得分:0)
将特定活动启动模式设为清单中的单个顶部。
<body>
<site-header>
<!-- NO <header> here! but there could be <header> instead of
<site-header> as long as there is only one of them. -->
<section>
<h1>Page title</h1>
<h2>Page slogan</h2>
</section>
<section>
<!-- Login and register. -->
<a href="./login.html">Login</a>
<a href="./register.html">Register</a>
<!-- Site search. -->
<input type="search">
</section>
</site-header>
</body>
像往常一样开始活动
android:launchMode = "singleTop"
它将带来现有的活动而不是创建一个新活动。
您可以在https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
了解有关启动模式的更多信息