本文主要讲解比较推荐的方式——广播接收者。通过广播接收者 ,在service中执行完耗时操作后,将结果以广播的形式发送,在所有的activity中注册广播,接收到结果后更新UI,这种方式比较简单。因为耗时的操作结果不需要以handler的方式发送到主线程,可以直接在子线程中发送广播,接收者始终运行在主线程中。
- Service.java
package com.example.servicecallback;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class ContentService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new LocalBinder();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public final class LocalBinder extends Binder {
public ContentService getService() {
return ContentService.this;
}
}
public