为什么我的BroadcastReceiver会在一段时间后停止接收 [英] Why my BroadcastReceiver stop receiving after a while

查看:274
本文介绍了为什么我的BroadcastReceiver会在一段时间后停止接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IntentService,需要很长时间才能完成,大约需要15分钟才能完成.这是一个从我的服务器获取新数据的同步过程.

I have a IntentService that does a long work, it takes about 15 minutes to be done. 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.

此活动将创建一个BroadcastReceiver,该BroadcastReceiver会拦截从服务发送的有关流程进度的消息.

This activity creates a BroadcastReceiver, that intercepts messages sent from the service about the process progress.

如果我让应用程序继续工作,请过一会儿,然后关闭屏幕.

If I leave the app doing it job, after a while the SO switch off the screen.

大约15分钟后,当我再次打开屏幕时,该服务已经完成,但进度似乎已过时. BroadcastReceiver已停止工作,并且活动尚未收到我的END OF SYNCHRONIZATION消息.

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. The BroadcastReceiver has stopped to work, and my END OF SYNCHRONIZATION message hasn't been received by the activity.

问题在于,在此消息中,我再次启动了主要活动,以使用户再次使用该应用程序.

The problem is that, at this message I start the main activity again to leave the user to use the app again.

我该如何解决?

推荐答案

我用 http://developer.android.com/intl/pt-br/guide/components/services.html#Foreground .

我的服务

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

我的活动

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

}

这篇关于为什么我的BroadcastReceiver会在一段时间后停止接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆