繁体   English   中英

如何确定应用程序在运行时是否处于休眠状态或在后台运行时是否调用了广播接收器?

[英]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?

我在清单中注册了BroadcatReceiver,目的是在电话铃响时使音频静音。 我希望BroadcastListener在3种情况下的行为有所不同:

  1. 如果在用户使用它时调用它。
  2. 如果按下主页按钮后在后台调用它。
  3. 如果在退出请求/ onBackButtonPressed()之后在后台调用它。

我想要这种能力来识别,因为我认为广播接收器会自动并以隐藏的方式启动发送通知的服务。 如果用户在明确离开应用程序后收到此通知,则很烦人。

这是我的一些尝试

  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;
}

}

我认为,在您的情况下,更简单的解决方案是在创建启动器活动时简单地将其设置为全局标志,而在销毁活动时将其关闭。 如果在将广播发送到接收器时您的应用未打开,则该标志将关闭,然后您可以进行相应操作。

如果您的应用程序是一个活动(并且我不认为它是一个InputMethodService),则可以重写Activity方法onPause()onResume() 如果您在其中一个中将某个静态标志设置为true,而在另一个中将其清除,则该标志将告诉您是否在屏幕上显示了最后一个活动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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