Android编程-IntentService使用广播与Activity通信

Android编程过程中,经常涉及到后台程序,一个长时间运行的后台程序使用Android提供的Service将是一个很好的选择,然而在众多Service中,IntentService最为常用,也最为简单,但是在使用IntentService时,将服务产生的结果反馈给Activity却不是一件容易的事情。一般情况下,ActivityService通信有两种方法,其一是通过绑定Binder对象,其二是通过broadcast(广播)的形式。本文将介绍一种属于第二种方案的特例,该方法使用IntentService作为后台Service,使用动态注册broadcast接收器作为通信桥梁,这样将可以省去很大一部分代码量,并且将通信变得更简单。

 

1.继承IntentService实现onHandleIntent方法

 

public class DownloadService extends IntentService {
    public static final String SERVICE_KEY_WORDS = 
                            "com.art.zok.DownloadService";
   
    public DownloadService() {
        super("***");
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        String url = intent.getStringExtra("***");
        downMethods(url);
    }
}

继承IntentService至少需要做两件事,第一,实现一个没有参数的默认构造器,并且在构造函数内调用含有String参数的父类(IntentService)构造器;第二,也是最重要的地方,就是实现OnHandleIntent(Intent intent)方法,该方法是IntentService的一个虚函数,当后台服务启动后,它将在后台服务的新线程调用,而不是主线程。我们实现该方法完成我们需要在后台完成的任务,如下载任务,其中intent参数将在启动服务时由我们创建并传递进来,这样在OnHandleIntent方法中我们就可以根据Intent的参数内容做出不同的后台响应。

启动IntentService可以使用ActivitystartService方法:

 

public class MainClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent i = new Intent(this, DownloadService.class);
        /*i.putExtra(****)此处可以添加相应的参数*/
        startService(i);
    }
}


2.实现广播和动态注册广播 


广播接收器动态注册与XML注册的最大区别是作用范围的大小,XML定义方法可以在整个系统范围内进行广播并接收,而动态注册仅当在本应用可以使用。因此在特定的ActivityService之间通信,使用动态注册广播接收更为方便。


public class MainClass extends Activity {
    public  String SERVICE_RECEIVER = "com.art.zok.receiver";
    private Intent intent;              
    private MsgReceiver msgReceiver;
    
    public void onCreate(Bundle savedInstanceState) {
        // 动态注册广播接收器
        msgReceiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SERVICE_RECEIVER);
        registerReceiver(msgReceiver, intentFilter);
        
        //启动后台服务
        intent = new Intent(this, DownloadService.class);
        /*intent.putExtra(****)此处可以添加相应的参数*/
        startService(intent);
    }
    
    @Override
    public void onDestroy() {
        
       // 停止服务
        stopService(intent);
        
        // 注销广播
        unregisterReceiver(msgReceiver);
        
        super.onDestroy();
    }
    
    public class MsgReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            /*处理接收到的广播内容*/
        }
    }
}


动态注册广播接收器的过程,首先需要实现一个广播接收器,如MsgReceiver,重写OnReceive方法,该方法的intent参数是后台Service发送广播时提供的反馈内容。接下来,注册接收器可以使用ActivityregisterReceiver方法,该方法接收一个BroadcastReceiver对象和一个IntentFilter对象,这里我们使用MsgReceiver的实例作为BroadcastReceiver对象参数,而IntentFilter 对象是用来标记(关联)接收器的,以便在发送广播后系统的正确找到接收器。这里我们实例一个IntentFilter对象,并调用addAction方法传递一个SERVICE_RECEIVER常量,表示我们接受具有SERVICE_RECEIVER常量表示的操作(Action)的广播,最后我们需要在onDestroy方法中停止服务和注销接收器。

当后台服务的(下载)任务完成时,我们就可以发送相应的广播来通知Activity,然后广播接收器就可以在OnReceive方法对接收到的Intent参数作出相应的响应,如UI更新。如下:


public class DownloadService extends IntentService {
        
        ***
    
    @Override
    protected void onHandleIntent(Intent intent) {
       ***
       downMethods(url);
       ***
       // 发送广播通知Activity
       Intent sendIntent = new Intent(MainClass.SERVICE_RECEIVER);
       ***
       getApplicationContext().sendBroadcast(sendIntent);
    }
}


这里我们创建一个具有
SERVICE_RECEIVER常量表示的操作(Action)的Intent对象,然后使用Context.SendBroadcast方法发送该Intent对象代表的广播。

到现在为止,使用上述方法已经完全可以做到ActivityService通信了,但是有一个问题,这里通信采用的是Intent进行数据内容传输,如果我们是下载任务的话,很有可以需要传递一个自定义的类,那么怎么进行传输呢?这就需要使用到Parcelable接口,实现该接口可以将你的自定义类序列化,然后放入Intent中进行传输。


3.实现Parcelable接口


假设我们下载任务完成后得到的是如下Item类的一组对象,那么我们将要怎么样将其传递给广播接收器呢?


public class Item {
    long id;
    String title;
    String img;
    int gallertClass;
    int count;
    int rcount;
    int fcount;
    long time;
    int size;
    *****
}


首先,必须要实现Item的序列化,序列化是通过实现Parcelable接口而实现的,这里我们将上面的类改造为ItemParcelable类,并实现Parcelable接口:

 

public class ItemParcelable implements Parcelable {
    long    id;
    String  title;
    String  img;
    int     gallertClass;
    int     count;
    int     rcount;
    int     fcount;
    long    time;
    int     size;
    
    public ItemParcelable() {}
    
    public long getId() {
        return id;
    }
    
    public long setId(long id) {
        this.id = id;
    }
    
    public String getTitle() {
        return title;
    }
    
    @Override
    public int describeContents() {
        return 0;
}

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(title);
        dest.writeString(img);
        dest.writeInt(gallertClass);
        dest.writeInt(count);
        dest.writeInt(rcount);
        dest.writeInt(fcount);
        dest.writeLong(time);
        dest.writeInt(size);
    }
    
       /**
	*实现Parcelable接口的类中,
	*必须有一个实现了Parcelable.Creator
	*接口的静态常量成员字段,并且它的名
	*字必须为CREATOR
	**/
    public static final Parcelable.Creator CREATOR = 
    new Parcelable.Creator() {
        @Override
        public ItemParcelable createFromParcel(Parcel in) {
            // 从包裹中读出数据
            ItemParcelable item = new ItemParcelable();
            item.setId(in.readLong());
            item.setTitle(in.readString());
            item.setImg(in.readString());
            item.setGallertClass(in.readInt());
            item.setCount(in.readInt());
            item.setRcount(in.readInt());
            item.setFcount(in.readInt());
            item.setTime(in.readLong());
            item.setSize(in.readInt());
            return item;
        }
        
        @Override
        public ItemParcelable[] newArray(int size) {
            return new ItemParcelable[size];
        }
    };
}


首先重写writeToParcel方法,将你的对象(ItemParcelable)序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从Parcel容器获取数据。接下来,重写describeContents方法,内容接口描述,默认返回0就可以。最后实例化静态内部对象CREATOR实现Parcelable.Creator publicstatic final Parcelable.Creator CREATOR接口。注意,其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。需重写本接口中的两个方法:createFromParcel(Parcelin)实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层,newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话即可(return new T[size]),供外部类反序列化本类数组使用,这里的T就是ItemParcelable。还需要注意的是writeToParcelcreateFromParcel方法中序列化的顺序必须相同。

4.
最后通过ItemParcelable传递数据


发送数据

@Override
protected void onHandleIntent(Intent intent) {
    ***
    ArrayList items = downMethods(***);
   
    // 发送广播通知Activity
    Intent sendIntent = new Intent(MainClass.SERVICE_RECEIVER);
    sendIntent.putParcelableArrayListExtra(SERVICE_KEY_WORDS, items);
    getApplicationContext().sendBroadcast(sendIntent);
}


接收数据

@Override
public void onReceive(Context context, Intent intent) {
    ArrayList items = intent
    .getParcelableArrayListExtra(DownloadService.SERVICE_KEY_WORDS);
    /**
     *for(ItemParcelable item : items) {
     *  item.getId();
     *	***
     *}
     **/
****
}


到现在为止,我们已经成功的完成ActivityService的通信任务,最后还需要弄清楚一个概念,IntentService在第一次调用startService时创建服务,如果在IntentService还没有完成后台任务(onHandleIntent函数)时再次调用startService函数,那么不再创建服务,而是在任务队列添加一个任务(也可以理解为消息,其实就是将Intent包含的信息添加到一个队列中等待调用OnHandleIntent调用),等待上次任务完成后再继续完成任务队列的下一条任务,当任务队列所有任务执行完毕后,则销毁服务,所以Service并不是自始至终都在后台运行,而只在有任务输入的时候才运行,这也保证了设备内存的充分利用。

你可能感兴趣的:(Android编程-IntentService使用广播与Activity通信)