处理连接变化,同时与IntentService加载数据 [英] Handling connection change while loading data with IntentService

查看:98
本文介绍了处理连接变化,同时与IntentService加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个小的小的应用程序,这将下载的数据从网上一个非常小的量。其他一切工作得很好,并下载正确的,但是,当连接的变化(我失去无线范围)的下载将无法完成,用户没有得到他们的数据。

So I have a small little app which downloads a very small amount of data from the net. Everything else works just fine and downloads properly, but when connection changes (I lose wifi range) the download won't complete and the user doesn't get their data.

我有一个想法如何处理这个问题。我将在我的主要活动一个BroadcastReceiver它与我的IntentService通信。当IntentService完成下载,然后我注销接收器。顶部这一切,我成立了一个BroadcastReceiver听连接的变化,如果连接是可用的,如果有一个连接,主要活动发送意图开始下载。在这里看到:

I have an idea how to handle this. I set up a BroadcastReceiver on my main Activity which communicates with my IntentService. When the IntentService completes the download, I then unregister the receiver. To top all this, I set up a Broadcastreceiver to listen connectivity changes and if connection is available, and if there is a connection, the main activity sends an Intent to start the download. See here:

主要活动:

public class Sample extends Activity {
    private BroadcastReceiver connectivityReceiver;
    private ResponseReceiver receiver;

    protected void onCreate(Bundle sis){
        super.onCreate(sis);


    IntentFilter intentFilter = new IntentFilter(
            "android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (Network.isOnline()) {
                fireUpDownloadingIntent();
            }
        }
        }, intentFilter);


    }

    public class ResponseReceiver extends BroadcastReceiver {
        public static final String ACTION_RESP = "com.irough.intent.action.URL_LOADED";

        @Override
        public void onReceive(Context context, Intent intent) {
              if(intent.getBooleanExtra(DLService.DOWNLOAD_COMPLETE, false) {
                  unRegisterReceiver(connectivityReceiver);
              }
        }

    }
}

DLService.java:

DLService.java:

public class DLService extends IntentService {
    public static final String DOWNLOAD_COMPLETE = "dlc";

    public DLService() {
        super("DLService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);

    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(DOWNLOAD_COMPLETE, true);
        sendBroadcast(broadcastIntent);

    }

}

关于code应该工作得很好,但有一个更容易或更好的方法来做到这一点?并不一定要在服务完成,AsyncTask的力逢我,当连接断开,这就是为什么把下载的行动服务。

The code about should work just fine, but is there an easier or better way to do it? Doesn't have to be done on Service, Asynctask force closes on me when connection drops and that's why put the download action to a service.

推荐答案

如果你失去了你的下载连接,我想你的下载将抛出某种异常。如果是我,我会简单地通知用户(使用href=\"http://developer.android.com/guide/topics/ui/notifiers/notifications.html\" rel=\"nofollow\"> Android的通知API的),并给予他们尝试重新下载数据的选项。

If you lose the connection in your download, I imagine your download will throw some sort of exception. If I were, I'd simply notify the user (using the android notification api), and give them the option to try to redownload the data.

preferably虽然,(而且违背了类似的问题我的previous后),你可以用我的新宠类在Android中,的 AsyncTaskLoader 。这听起来像它究竟是适合你想要做什么该法案。 Bascially,如果有一个错误的下载,只需要您的装载机返回null。然后在你的活动你onLoaderFinished钩,你需要的问候做告知用户什么都。请注意,这个类是仅适用于API等级3以上,但仍然可以通过较低的API级别通过的 Android的兼容性包

Preferably though, (and contrary to my previous post in a similar question), you could use my new favorite class in the android, the AsyncTaskLoader. It sounds like it exactly fits the bill for what you want to do here. Bascially, if there's an error downloading, just have your loader return null. Then in your onLoaderFinished hook in your activity, do what ever you need to do in regards to informing the user. Note that this class is only available to API levels 3 and above, but can still be accessed by lower API levels through the android compatibility package.

这篇关于处理连接变化,同时与IntentService加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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