1

我一直在到处寻找,但只是想不出如何让一个正在运行的Android应用程序的电流(顶部)活动。如何找出从onMessageReceived(的FirebaseMessagingService)当前(顶部)活动?

好吧,我的情况是我收到了火力地堡云消息数据有效载荷而应用程序是在前台和onMessageReceived(的FirebaseMessagingService的子类)被调用。我需要找出用户正在查看的屏幕,然后决定解除(完成())它或发送一些数据(通过Extras/Bundle)并刷新视图。

那么,如何才能知道什么是当前视图/活动和讨论或发送一些数据,使视图刷新?

谢谢!

回答

1

别人张贴的正确答案,然后删除了它,所以我会再重新发布:

您不需要确定最重要的活动。您的每项活动都应监听来自FirebaseMessagingService的某些信号并采取适当的措施。该信号可能是在LocalBroadcastManager一个Intent广播,或者你可以使用某种事件总线。

1

我们得到当前前台运行活动

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
ComponentName cn = am.getRunningTasks(1).get(0).topActivity; 

所需的权限

<uses-permission android:name="android.permission.GET_TASKS"/> 
+0

谢谢!我尝试了代码。两个问题:(1)getRunningTasks已被弃用。 (2)我如何让活动完成()它? – ikevin8me

+0

是你的应用程序的活动或可能是任何应用程序? – jagapathi

+0

我得到的活动名称(即组件名称)正是我想要的/预期的。现在我需要完成()它。我怎么做? – ikevin8me

0

正如跟进凯文Krumwiede的已接受的答案,这里有一些实施细节的一种可能的方式来跟随他的做法(任何错误都是我的):

创建可重用的BroadcastReceiver

public class CurrentActivityReceiver extends BroadcastReceiver {  
    private static final String TAG = CurrentActivityReceiver.class.getSimpleName(); 
    public static final String CURRENT_ACTIVITY_ACTION = "current.activity.action"; 
    public static final IntentFilter CURRENT_ACTIVITY_RECEIVER_FILTER = new IntentFilter(CURRENT_ACTIVITY_ACTION); 

    private Activity receivingActivity; 

    public CurrentActivityReceiver(Activity activity) { 
     this.receivingActivity = activity; 
    } 

    @Override 
    public void onReceive(Context sender, Intent intent) { 
     Log.v(TAG, "onReceive: finishing:" + receivingActivity.getClass().getSimpleName()); 

     if (<your custom logic goes here>) { 
      receivingActivity.finish(); 
     } 
    } 
} 

实例化,并使用广播接收器在每个活动的

public class MainActivity extends AppCompatActivity { 

    private BroadcastReceiver currentActivityReceiver; 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     currentActivityReceiver = new CurrentActivityReceiver(this); 
     LocalBroadcastManager.getInstance(this). 
      registerReceiver(currentActivityReceiver, CurrentActivityReceiver.CURRENT_ACTIVITY_RECEIVER_FILTER); 
    } 

    @Override 
    protected void onPause() { 
     LocalBroadcastManager.getInstance(this). 
      unregisterReceiver(currentActivityReceiver); 
     currentActivityReceiver = null; 
     super.onPause(); 
    } 
} 

最后,把你的FirebaseMessagingService内的适当广播

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Intent localMessage = new Intent(CurrentActivityReceiver.CURRENT_ACTIVITY_ACTION); 
     LocalBroadcastManager.getInstance(MyApplication.this). 
      sendBroadcast(localMessage); 
    } 

} 

此代码寄存器和注销currentActivityReceiver以这样的方式,以确保广播接收机是活跃,只有when the Activity is active.

如果您有大量在您的应用程序活动,你可能要创建一个抽象基类的活动,并把和的onResume在onPause代码是,和你的其他活动从类继承。

您还可以将数据添加到onMessageReceived(例如localMessage.putExtra())中名为“localMessage”的Intent中,并稍后在接收器中检索该数据。凯文的回答

一个优势是,他的方法不需要任何额外的权限(如GET_TASKS)。另外,正如Kevin指出的那样,除了BroadcastReceiver之外,还有其他更便捷的方式来传递消息(例如EventBusOtto)。恕我直言,这些都很好,但他们需要一个额外的库,这将增加一些方法计数开销。而且,如果您的应用程序已经在很多其他地方使用BroadcastReceivers,则出于审美和维护的原因,您可能会觉得您不希望在应用程序中传递消息。 (也就是说,Eve​​ntBus非常酷,我感觉它比BroadcastReceivers更简单。)

相关问题