简体   繁体   中英

How can I determine if a broadcast receiver was invoked when the app was running as opposed to when the app was dormant or in the background?

I have a BroadcatReceiver registered in the manifest for the purpose of silencing audio when the phone rings. I would like the BroadcastListener to behave differently in 3 situations:

  1. If it was invoked while the user was using it.
  2. If it was invoked while in the background after the home button was pressed.
  3. If it was invoked while in the background after an exit request/ onBackButtonPressed().

I want this ability to discern, because I think the broadcast receiver automatically and in a hidden way starts a service which sends a notification. If the user receives this notification after explicitly leaving the app it is annoying.

Here are some of my attempts

  public class MyBroadcastReceiver extends BroadcastReceiver {
    //private final static Global global = Global.getInstance();
    private static final String TAG = MyBroadcastReceiver.class.getName();

   @Override
   public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "broadcast recieved");
    //Log.d(TAG, "context package name is: " + context.getPackageName());

    Bundle extras = intent.getExtras();

    if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);
        Log.d(TAG, "State : " + state);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
        {

            Log.d(TAG, "context package name: " + context.getPackageName());

            Global global = Global.getInstance();
            global.shutUp();

            Log.d(TAG, "global package name: " + global.getPackageName());

            String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.d(TAG, "Calling number: " + phoneNumber);





            //TODO try to condition this if receiver activated from outside the application 
            if(isAppOnForeground(context))
            {
                Log.d(TAG, "app is in forgeround");
            }
            else
            {
                Log.d(TAG, "app is not in forgeround");
            }

            if(isMyServiceRunning(context))
            {
                Log.d(TAG, "service running");
            }
            else
            {
                Log.d(TAG, "service isn't running");
            }


            //global.stopService(new Intent(ReturnService.class.getName()));
            //Log.d(TAG, "requested return service stoped");

        }
    }

    //I think this return is imperative so that return service isn't invoked 
    return;
}


private boolean isAppOnForeground(Context context) 
{
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

    if (appProcesses == null) 
    {
      return false;
    }

    final String packageName = context.getPackageName();

    for (RunningAppProcessInfo appProcess : appProcesses) 
    {
      if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) 
      {
        return true;
      }
    }
    return false;
  }


private boolean isMyServiceRunning(Context context) 
{
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
    {
        if (ReturnService.class.getName().equals(service.service.getClassName())) 
        {
            return true;
        }
    }
    return false;
}

}

I think a simpler solution in your case would be to simply set on a global flag when your launcher activity is created and off when it is destroyed. If your app is not open when a broadcast is sent to your receiver, that flag will be off and you can then operate accordingly.

If your application is an activity, (and I do not think it is eg an InputMethodService) you can override the Activity methods onPause() and onResume() . If you set some static flag to true in one of them and clear it in the other, that flag will tell you whether the last activity is on the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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