简体   繁体   English

通过收到通知重新创建活动

[英]Recreating activity with getting a notification

There is the following task: 有以下任务:

There is the first activity (for example, "Activity A"), and the second activity ("Activity B"). 有第一个活动(例如“活动A”)和第二个活动(“活动B”)。 Also there is some Service (just "Service) for catching notifications from my server. I'd like to realize the following algorithm: 另外,还有一些服务(简称“服务”)用于捕获来自服务器的通知。我想实现以下算法:

  1. User is on "Activity A" or "Activity B" 用户处于“活动A”或“活动B”中
  2. "Service" catches some notification from my server “服务”从我的服务器捕获了一些通知
  3. If user is on "Activity A" then service should create notification on Notification Bar; 如果用户在“活动A”上,则服务应在通知栏上创建通知; if user is on "Activity B" already the service should just recreate "Activity B" without creating a notification. 如果用户已经在“活动B”上,则该服务应仅重新创建“活动B”而不创建通知。
  4. If notification has been created user could click by it and go to "Activity B" 如果已创建通知,则用户可以单击该通知并转到“活动B”

I've made all steps without the third one, because I don't know how I can get the current activity. 我已完成所有步骤而没有第三步,因为我不知道如何获得当前活动。 Please, tell me, is there any way to detect which activity is current? 请告诉我,有什么方法可以检测到当前正在进行的活动吗? Thanks. 谢谢。

There is no "legit" way of getting the Activity the user is on. 没有“合法”的方式来获取用户处于Activity状态。

You can always do a hack : 您可以随时进行破解:

  • declare a static boolean sVisible member in your ActivityB class ActivityB类中声明一个static boolean sVisible成员
  • in onStart() , set that flag to true onStart() ,将该标志设置为true
  • in onStop() , set that flag to false onStop() ,将该标志设置为false
  • in your Service check if(ActivityB.sVisible) 在您的Service检查if(ActivityB.sVisible)

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

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