Start IntentService from Activity and refresh Activity when IntentService is finished

后端 未结 5 1123
-上瘾入骨i
-上瘾入骨i 2020-12-08 05:04

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

相关标签:
5条回答
  • 2020-12-08 05:12

    None of the other answers references the official android documentation

    https://developer.android.com/training/run-background-service/report-status.html

    that states clearly that for the Activity-IntentService communication "The recommended way to send and receive status is to use a LocalBroadcastManager, which limits broadcast Intent objects to components in your own app"!

    0 讨论(0)
  • 2020-12-08 05:13

    When the IntentService completes, it should use LocalBroadcastManager to send an intent to any registered activity.

    The IntentService will contain code like this:

    private void sendBroadcast() {
        Intent intent = new Intent("myBroadcastIntent");
        intent.putExtra("someName", someValue);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
    

    The activity receiving the notification will contain code like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String someValue = intent.getStringExtra("someName");
                // ... do something ...
            }
        };
        LocalBroadcastManager.getInstance(this)
            .registerReceiver(receiver, new IntentFilter("myBroadcastIntent"));
    }
    

    For more depth, see the blog post Using LocalBroadcastManager In Service To Activity Communications.

    0 讨论(0)
  • 2020-12-08 05:23

    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:

    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:

    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:

    Bundle resultBundle = new Bundle();
    ResultReceiver resRec = intent.getParcelableExtra("resReceiver");
    
    // Do some work then put some stuff in resultBundle here
    
    resRec.send(12345, resultBundle);
    
    0 讨论(0)
  • 2020-12-08 05:29

    I think the event bus is the way to go. Simple and effective interprocess communication.

    • http://square.github.io/otto/
    • https://github.com/greenrobot/EventBus
    0 讨论(0)
  • 2020-12-08 05:31

    I would suggest using a Broadcast Receiver in the The Activity waiting for the result. Your Service would just use sendBroadcast with a custom Intent.

    0 讨论(0)
提交回复
热议问题