简体   繁体   English

应用程序在后台时的 Android 广播接收器

[英]Android broadcast receiver when the app is in background

I'm facing a problem with the android broadcast receiver.我遇到了android广播接收器的问题。 I have a login page and after a successful login the app goes to another activity.我有一个登录页面,成功登录后,应用程序进入另一个活动。 And it contains a broadcast receiver.它包含一个广播接收器。 I am sending a broadcast once a separate service completes its action.一旦单独的服务完成其操作,我将发送广播。 The app works fine in a happy day scenario.该应用程序在快乐的一天场景中运行良好。

My problem here is, after i entered the username / password and hit the login button i send the app to the background.我的问题是,在我输入用户名/密码并点击登录按钮后,我将应用程序发送到后台。 But when the app is in background that broadcast receiver wont receive the broadcast which i'm sending.但是当应用程序在后台时,广播接收器不会接收到我正在发送的广播。

Anyone have a clue regarding this issue ?有人知道这个问题吗?

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks in advance.提前致谢。

Are you unregistered the broadcast receiver in any of the activities onPause/OnStop/OnDestroy method?您是否在任何活动 onPause/OnStop/OnDestroy 方法中取消注册广播接收器? if yes then you will not receive the broadcast.如果是,那么您将不会收到广播。 make sure you have register the Broadcast receiver in the AndroidManifest.确保您已在 AndroidManifest 中注册广播接收器。 and not unregistered.并且没有未注册。 if you wanted to receive intent in the background then you should not unregistered it.如果你想在后台接收意图,那么你不应该取消注册它。

Are you unregistered the broadcast receiver in any of the activities onPause/OnStop/OnDestroy method?您是否在任何活动 onPause/OnStop/OnDestroy 方法中取消注册广播接收器? if yes then you will not receive the broadcast.如果是,那么您将不会收到广播。 make sure you have register the Broadcast receiver in the AndroidManifest .确保您已在AndroidManifest中注册广播接收器。 and not unregistered.并且没有未注册。 if you wanted to receive intent in the background then you should not unregistered it.如果你想在后台接收意图,那么你不应该取消注册它。 0 0

If you start separate service in another Activity right after you hit the login button ?如果您在点击登录按钮后立即在另一个活动中启动单独的服务? Because service is the entity that ment to run on the background.因为服务是在后台运行的实体。

If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, the application will be in background.如果您的回答是肯定的,我认为您应该考虑检查活动的生命周期,因为当您点击登录按钮时,应用程序可能会在后台运行。 Now, at this point activity inserts to onPause() .现在,此时活动插入到onPause() Therefore your second activity is not active and as a result can't call to the service.因此,您的第二个活动未激活,因此无法调用该服务。

Is you start separate service in another Activity right after you hit the login button ?您是否在点击登录按钮后立即在另一个 Activity 中启动单独的服务? Because service is the entity that ment to run on the background.因为服务是在后台运行的实体。

If your answer is yes, i think you should consider to check the lifecycle of the activity, because there is possibility that when you hit the login button, you send your application on the background.如果您的回答是肯定的,我认为您应该考虑检查活动的生命周期,因为当您点击登录按钮时,您可能会在后台发送您的应用程序。 Now, at this point activity inserts to onPause.现在,此时活动插入到 onPause。 Therefore your second activity is not active and as a result can't call to the service.因此,您的第二个活动未激活,因此无法调用该服务。

I hoped it will you help to resolve your problem :)我希望它会帮助你解决你的问题:)

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

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