Note that there are some explanatory texts on larger screens.

plurals
  1. POHow 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?
    primarykey
    data
    text
    <p>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:</p> <ol> <li>If it was invoked while the user was using it.</li> <li>If it was invoked while in the background after the home button was pressed.</li> <li>If it was invoked while in the background after an exit request/ onBackButtonPressed().</li> </ol> <p>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. </p> <p>Here are some of my attempts</p> <pre><code> 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&lt;RunningAppProcessInfo&gt; appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND &amp;&amp; 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; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload