0

我需要为应用程序设计一个更新服务,这个更新服务将从线索中提取数据,如果有新的更新,它应该更新 GUI。第一部分是直截了当的,但对于第二部分,最好的方法是什么?

我正在考虑有一个自定义意图接收器,它将在应用程序中注册,它会告诉活动再次加载内容。此外,如果应用程序关闭,我需要显示来自活动的自定义对话框,告知更新。

我需要您的反馈,如果任何机构有类似的示例项目,请分享它以查看实施细节。

谢谢。

4

2 回答 2

0

一种可能的方法是将您的数据同步到ContentProvider,当您的活动开始时,注册以通过以下方式观察该提供程序的更改

ContentResolver.registerContentObserver()

我记得 jumpnote 应用程序使用了这个。关联

于 2012-06-07T07:39:38.783 回答
0

此更新服务将从线索中提取/下载数据

从服务下载

这里最大的问题是:如何从服务更新我的活动?在下一个示例中,我们将使用您可能不知道的两个类:ResultReceiver 和 IntentService。ResultReceiver 允许我们从服务更新我们的线程;IntentService 是 Service 的一个子类,它产生一个线程来从那里做后台工作(你应该知道一个 Service 实际上在你的应用程序的同一个线程中运行;当你扩展 Service 时,你必须手动产生新线程来运行 CPU 阻塞操作) .

下载服务可能如下所示:

public class DownloadService extends IntentService {
    public static final int UPDATE_PROGRESS = 8344;
    public DownloadService() {
        super("DownloadService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        String urlToDownload = intent.getStringExtra("url");
        ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
        try {
            URL url = new URL(urlToDownload);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk");

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                Bundle resultData = new Bundle();
                resultData.putInt("progress" ,(int) (total * 100 / fileLength));
                receiver.send(UPDATE_PROGRESS, resultData);
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Bundle resultData = new Bundle();
        resultData.putInt("progress" ,100);
        receiver.send(UPDATE_PROGRESS, resultData);
    }
}

将服务添加到您的清单中:

<service android:name=".DownloadService"/>

如果有新的更新,它应该更新 GUI

活动将如下所示:

// 像第一个例子一样初始化进度对话框

// 这就是你触发下载器的方式

mProgressDialog.show();
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra("url", "url of the file to download");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(intent);

这是 ResultReceiver 来玩的:

private class DownloadReceiver extends ResultReceiver{
    public DownloadReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        if (resultCode == DownloadService.UPDATE_PROGRESS) {
            int progress = resultData.getInt("progress");
            mProgressDialog.setProgress(progress);
            if (progress == 100) {
                mProgressDialog.dismiss();
            }
        }
    }
}
于 2012-06-07T07:32:57.197 回答