0

I have an application-wide BroadcastReceiver declared in the AndroidManifest.xml. After a broadcast has been processed in the receiver, I want to start an activity to handle the event. There is two options:

a) if the target activity is running, simply update it;

b) if the target activity is not running, show notification to user, so he could click it and launch the activity.

But the receiver doesn't know if the activity is running or not. I was thinking of defining static variable running inside the activity, which updates in onStart and onPause. But is this a reliable solution? I guess not.

What is the most appropriate way?

user1256821
  • 1,158
  • 3
  • 15
  • 35

1 Answers1

0
    public class MyBroadcastReceiver extends BroadcastReceiver
    {
public boolean checkApp;

        @Override
        public void onReceive(Context context, Intent intent) 
        {
    for (RunningAppProcessInfo appProcess : appProcesses) {
                    if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                            && appProcess.processName
                                    .equals("-------> Ur package ----> example--[com.emdsys.android.pacs]")) {


                        List<ActivityManager.RunningTaskInfo> taskInfo = activityManager
                                .getRunningTasks(1);



                        if (taskInfo.get(0).topActivity
                                .getClassName()
                                .toString()
                                .equals("-----> UR Activity Name-------(-ex)[com.emdsys.android.pacs.activity.TabActivityPacs]")) {
checkApp = true;

    //update ur activity what ever u want.
                        }

                        ComponentName componentInfo = taskInfo.get(0).topActivity;
                        componentInfo.getPackageName();

                    }
    }
if(!checApp){

            Log.d("ME", "Notification started");
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, [ur Activity].class), 0);

            NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
setContentIntent(contentIntent).build();

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
}
        }