简体   繁体   English

如何将Android应用程序在后台运行到前台

[英]Ways to bring an Android app in background to foreground

Here is the scenario: 这是场景:

AndroidManifest.xml defines a single Activity with android:launchMode="singleTask" . AndroidManifest.xml使用android:launchMode="singleTask"定义了一个Activity android:launchMode="singleTask" (This means there should be a single activity in the stack throughout the entire application lifecycle, right ?) (这意味着整个应用程序生命周期中堆栈中应该有一个活动,对吧?)

During Activity.onCreate() , a broadcast receiver is programmatically created and listens for incomming SMS. Activity.onCreate()期间,以编程方式创建广播接收器并侦听发送短信。 The receiver remains active even after Activity.onPause() by design . 接收器即使在保持有效Activity.onPause() 由设计

When the user is done with the application, he presses the device Home button which calls Activity.onPause() and the application disappears. 当用户完成应用程序时,他按下设备Home按钮,该按钮调用Activity.onPause()并且应用程序消失。 The device shows then the Android home screen. 然后设备显示Android主屏幕。

Upon receiving SMS, the broadcast receivers receives SMS and tries to show up the Activity via: 收到短信后,广播接收机接收短信并尝试通过以下方式显示活动:

Intent it = new Intent(context, Akami.class);
it.setAction(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_LAUNCHER);
it.setComponent(new ComponentName(context.getPackageName(), "MyActivity"));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);

However, the activity is NOT showed up to the user. 但是,活动不会显示给用户。

  • a) Why ? a)为什么?
  • b) What are the possible ways to bring an Activty to foreground ? b)将Activty带到前台的可能方法有哪些?

In MyMainActivity definition (AndroidManifest.xml): MyMainActivity定义(AndroidManifest.xml)中:

<intent-filter>
 <action android:name="intent.my.action" />
 <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Programmatically bringing application to foreground: 以编程方式将应用程序引入前台

Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(), MyMainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(it);

Note: context.startActivity(it) would NOT work when the context object is same as the activity one wants to bring up. 注意:当context对象与想要调用的活动相同时, context.startActivity(it)将不起作用。

Yes, what you are saying is correct, have a BroadcastReciever and fire an intent to your Activity to bring it to foreground. 是的,你说的是正确的,有一个BroadcastReciever并激活你的活动的意图,使其前景。 Word of caution however regarding the Activity life cycle. 但是关于活动生命周期的注意事项。

Android OS can take your activity from onPause() to onStop() and onDestroy() depending on system resources. Android操作系统可以根据系统资源将您的活动从onPause()转移到onStop()和onDestroy()。 So in such a case, your calling Activity will restart again, so take precautions there. 所以在这种情况下,你的调用Activity会再次重启,所以在那里采取预防措施。 Else, very easy to run into NullPointerExceptions 否则,很容易遇到NullPointerExceptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM