如果活动正在运行,则发送LocalBroadcast,然后从后台服务发送通知

时间:2015-10-30 07:58:28

标签: android android-notifications localbroadcastmanager

如果Activity正在运行,我需要使用LocalBroadcast从后台服务更新我的Activity。如果应用程序或活动目前没有运行,我想添加待处理通知。

我经历了以下帖子 -

Check if activity is running from service

Activity or Notification via Ordered Broadcast

我有以下查询 -

  • 如何检查Application / Activity是否正在从后台服务运行?
  • 如果我在我的活动中在onPause()和onResume()中注册和取消注册广播监听器,我还需要检查活动是否正在运行?
  • 根据上面的链接#2 - If the activity is not on-screen, its receiver will not be registered, so the event will go to the default handler, in the form of your manifest-registered BroadcastReceiver, which will raise the Notification - 如果可能,如何实现这一目标?

如果还有其他解决方案,请分享。

1 个答案:

答案 0 :(得分:1)

过去一周我遇到了同样的情况。我找到了一个可能对你有帮助的更好的解决方案。

查找活动是否是服务中当前正在运行的活动

boolean isNotificationRequired = true;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("TEST", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();

添加清单文件

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

现在,如果活动是当前正在运行的活动,请执行操作。

if(taskInfo.get(0).topActivity.getClassName().equals(YOUR_CURRENT_ACTIVITY.class.getName())
{
     //Perform the Operations
     isNotificationRequired = false;
}

现在仅在isNotificationRequired为真时发送通知。

if(isNotificationRequired){
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification Title")
                .setContentText("Notification Message");
        PendingIntent notifyIntent = PendingIntent.getActivity(this, requestcode,
                resultIntent, PendingIntent.FLAG_ONE_SHOT);
        mBuilder.setContentIntent(notifyIntent);
        mBuilder.setAutoCancel(true);
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE
                | Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(requestcode, mBuilder.build());    
}

否则,发送广播并更新您的活动。

(对我而言,如果我发送现有的意图,它在接收器中没有正确接收。所以我创建了新的意图并将此现有意图的数据传递给此newIntent的putExtras()。)

else {
            Log.i("TEST", "Sending broadcast to activity");
            Intent newIntent = new Intent();
            newIntent.setAction("TestAction");
            sendBroadcast(newIntent);
     }

然后在您的活动中通过Creating broadcat receiver处理广播。别忘了实例化。无需在manifest.xml中提及您的接收器。

public class YourCurrentRunningActivity extends Activity {
    YourBroadcastReceiver receiver = new YourBroadcastReceiver();

    protected void onCreate(Bundle savedInstanceState) {
    (this).registerReceiver(receiver, new IntentFilter("TestAction"));

     public class YourBroadcastReceiver extends BroadcastReceiver {

         @Override
         public void onReceive(Context arg0, Intent arg1) {
              // Perform the Actions u want.
         }
     }
}

然后你可以在onStop()/ onPause()/ onDestroy()中取消注册接收器,如下所示:

this.unregisterReceiver(receiver);