从活动开始IntentService当IntentService完成刷新活动IntentService

2023-09-12 04:21:21 作者:≒赱ζц斯文

在我的Andr​​oid应用程序,我有一个简单的列表视图适配器。有一个沉重的查询,这是为了填充列表视图的数据。所以我把它运行在另一个线程的IntentService。

In my Android application, I have a simple list view with adapter. There's a heavy query which is to fill the list view with data. So I put it to an IntentService that runs in another thread.

该IntentService正常运行分开,对自己,只是为了查询一些数据,并把它插入SQLite数据库。

The IntentService is normally running separately, on its own, just to query some data and insert it into the SQLite database.

但现在我想有以下可能:

But now I would like to have the following possibility:

在活动开始startService()的IntentService。 的IntentService做了繁重的工作。 当IntentService完成后,将应通知的活性有关的结果,以使活动可以刷新以显示新的数据。

这可能吗?我读了很多的问题在这里对堆栈溢出关于这一主题。但在每一个问题,还有一个解决方案。所以我想问问大家:哪种解决方案是最适合我的目的

Is this possible? I read a lot of questions here on Stack Overflow on this topic. But in every question, there was another solution. So I want to ask you all: Which solution is the best for my purpose?

的IntentService绑定到活动似乎没有是最好的解决方案,因为有可能是与该活动的配置变更等冲突正确? This博客文章建议使用AIDL与parcelables的 - 这听起来很复杂我。还有一个更简单的方法,是不是? 在一个可以设置在活动的广播接收机和火这个广播的IntentService当它完成。 有人说,你应该使用createPendingResult()传递一个PendingIntent 来的IntentService。如果IntentService发现PendingIntent在其临时演员,并利用它引发onActivityResult()在活动。难道这就是方法来选择? Binding the IntentService to the Activity does not seem to be the best solution as there might be conflicts with configuration changes of the activity etc. Correct? This blog post suggests using AIDL with Parcelables - which sounds very complex to me. There is an easier way, isn't it? One could set up a broadcast receiver in the activity and fire this broadcast in the IntentService when it is finished. Some people say you should use createPendingResult() to pass a PendingIntent to the IntentService. If the IntentService finds that PendingIntent in its extras, it uses this to trigger off onActivityResult() in the Activity. Is this the way to choose?

推荐答案

作为一个例子,我使用 ResultReceiver 来调用 notifyDataSetChanged()我的活动(的适配器延伸 ListActivity )。它可以适应做任何你需要的。

As an example, I use a ResultReceiver to call notifyDataSetChanged() on the adapter of my Activity (which extends ListActivity). It can be adapted to do whatever you need.

ResultReceiver code:

ResultReceiver code:

public class MyResultReceiver extends ResultReceiver {

    private Context context = null;

    protected void setParentContext (Context context) {
        this.context = context;
    }

    public MyResultReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult (int resultCode, Bundle resultData) {

        // Code to process resultData here

        ((BaseAdapter) ((ListActivity)context).getListAdapter()).notifyDataSetChanged();
    }
}

MyActivity code:

MyActivity code:

public class MyActivity extends ListActivity {

    private MyResultReceiver theReceiver = null;

    ...

    private void callService () {
        theReceiver = new MyResultReceiver(new Handler());
        theReceiver.setParentContext(this);
        Intent i = new Intent("com.mycompany.ACTION_DO_SOMETHING");

        // Code to define and initialize myData here

        i.putExtra("someData", myData);
        i.putExtra("resReceiver", theReceiver);
        startService(i);

    }
}

IntentService code:

IntentService code:

Bundle resultBundle = new Bundle();
ResultReceiver resRec = intent.getParcelableExtra("resReceiver");

// Do some work then put some stuff in resultBundle here

resRec.send(12345, resultBundle);