[英]Why my BroadcastReceiver stop receiving after a while
I have a IntentService
that does a long work, it takes about 15 minutes to be done. 我有一个做长期工作的IntentService
,大约需要15分钟。 It is a synchronization process to get new data from my server. 这是从我的服务器获取新数据的同步过程。
When this service starts, I start a activity too, to display the progess. 当这个服务启动时,我也开始一个活动,以显示该进程。
This activity creates a BroadcastReceiver
, that intercepts messages sent from the service about the process progress. 此活动创建一个BroadcastReceiver
,用于拦截从服务发送的有关进程进度的消息。
If I leave the app doing it job, after a while the SO switch off the screen. 如果我让应用程序完成它的工作,一段时间后SO关闭屏幕。
When I switch on the screen again, after about 15 minutes, the service has been already done, but the progress appears to be out of date. 当我再次打开屏幕时,大约15分钟后,服务已经完成,但进度似乎已经过时。 The BroadcastReceiver
has stopped to work, and my END OF SYNCHRONIZATION
message hasn't been received by the activity. BroadcastReceiver
已停止工作,活动尚未收到END OF SYNCHRONIZATION
消息。
The problem is that, at this message I start the main activity again to leave the user to use the app again. 问题是,在此消息中,我再次启动主要活动,让用户再次使用该应用程序。
How can I solve this? 我怎么解决这个问题?
Broadcast Receivers are not made to do long duration work. 广播接收器不能进行长时间的工作。
The lifetime of a broadcast receiver lasts about 10-15 seconds. 广播接收器的寿命大约持续10-15秒。
A recommended or typical use of a broadcast receiver is to 广播接收机的推荐或典型用途是
In your case you should start a service from your broadcast Receiver and do all the work in that service. 在您的情况下,您应该从广播接收器启动服务并完成该服务中的所有工作。
I solved with this http://developer.android.com/intl/pt-br/guide/components/services.html#Foreground . 我解决了这个http://developer.android.com/intl/pt-br/guide/components/services.html#Foreground 。
My service 我的服务
public class MyService extends Service {
public interface MyCallback {
void onProgress(int progress);
}
public class MyBinder {
public MyService getService() {
return MyService.this;
}
}
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public void make(MyCallback callback) {
Notification n = new Notification.Builder(this)
.setContentTitle("Processing")
.getNotification();
startForeground(666 /*some ID*/, n);
try {
callback.onProgress(0);
// do the hard sutff and report progress
callback.onProgress(100); // report 100%
} finally {
stopForeground(true);
}
}
}
My activity 我的活动
public MyActivity extends Activity implements ServiceConnection, MyService.MyCallback {
@Override
protected onStart() {
super.onStart();
// 1 - bind service to this activity
Intent i = new Intent(this, MyService.class);
this.bindService(i, this, BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName componentName, final IBinder iBinder) {
// 2 - when the service was binded, starts the process asynchronous
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
((MyService.MyBinder) iBinder).getService().make(MyActivity.this);
return null;
}
}.execute();
}
@Override
public void onProgress(int progress) {
// 3 - when to callback is fired, update the UI progress bar
runOnUiThread(new Runnable() {
@Override
public void run() {
// call ProgressBar.setProgress(progress);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.