android远程服务service

来源:互联网 发布:数据库 主键 外键 编辑:程序博客网 时间:2024/04/29 14:04

android Service,大部分应用使用到service是在执行一种与用户不需要交互的任务,这种任务只需要放到后端服务中去执行,我们需要呈献给前段展示的可能是这个任务的执行状态。比如说从远程下载一个较大的数据包,前端我可能只需要向用户展现一个下载按钮,一个下载的进度。

当然可以将这个service看做是一个没有界面的activity。

我们在没有界面的activity中创建一个线程,去执行耗费时间资源的任务。

创建远程服务需要Parcelable,就当做是一个实体类对象好了...主要方法:writeToParcel 写值、new Parcelable.Creator<Queue>() 里 public Queue createFromParcel(Parcel source)  方法读值。public Queue[] newArray(int size)返回任务数?

在activity中绑定service:

/* 遥控服务连接 */    private ServiceConnection remoteConnection = new ServiceConnection() {         public void onServiceConnected(ComponentName name, IBinder service) {             /* 遥控服务的AIDL  */            remoteService = MyServiceBinder.Stub.asInterface(service);         }         public void onServiceDisconnected(ComponentName name) {            remoteService = null;         }     }; 


 

    Intent intent=new Intent();intent.setClass(Activity.this, Service.class);startService(intent);/* 遥控服务bind */bindService(intent, remoteConnection,Context.BIND_AUTO_CREATE);


写得很简洁...就先做个记录,回头写个例子发上来~!

 

 

 

原创粉丝点击