Android-基础-service

参考:http://blog.csdn.net/guolin_blog/article/details/11952435

一、基础用法

startService 启动

stopService 停止

bindService(bindIntent, connection, BIND_AUTO_CREATE) 绑定connection

unbindService(connection) 解绑connection

注意:Service运行在主线程中,如果需要做耗时操作,需要新起线程!

二、生命周期


onCreate
service被创建的时候调用。只执行一次。

onStartCommand
每次执行startService的时候都会被调用。

onDestroy
service被销毁

三、activity与service之间的通信

step1:service中定义Binder实例,通过复写onBind方法返回bind实例。

public class MyService extends Service {

private MyBinder mBinder = new MyBinder();

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

class MyBinder extends Binder {
    public void playMusic() {
        Log.d("TAG", "playMusic executed");
    }

}

}

step2:activity中定义connection实例,在onServiceConnected中获得Service中定义好的Binder实例。

private ServiceConnection playMusicConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        myBinder = (MyService.MyBinder) service;
        myBinder.playMusic();
    }
};

step3:activity中建立绑定关系

Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent,playMusicConnection, BIND_AUTO_CREATE);

step4:解除绑定

unbindService(playMusicConnection);

四、start+stop 、bind+unbind必须配套使用

如果Service同时被start和bind,必须在stop和unbind后才会被销毁 这关系到service onDestory方法什么时候被调用

五、远程Service

注册Service的时候将它的android:process属性指定成:remote,该service即具备了远程能力。
远程service已经不和activity在同一个进程,如果当前进程为com.example.test。那么远程service所在的进程一般为com.example.test:remote
既然service已经不在同样的进程中,那么service中定义的binder就无法被activity获取到。
这时候,activity和service之间的就需要用到跨进程通信,即使用AIDL实现IPC。
AIDL(Android Interface Definition Language)是Android接口定义语言的意思,它可以用于让某个Service与多个应用程序组件之间进行跨进程通信,从而可以实现多个应用程序共享同一个Service的功能。

六、 AIDL实现

6.1 新建AIDL文件 MyAIDLService.aidl ,定义接口

interface MyAIDLService {
String toUpperCase(String str); //实现字符串大写转换
}

6.2 Service实现AIDL接口

public class MyService extends Service {

......

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

MyAIDLService.Stub mBinder = new Stub() {

    @Override
    public String toUpperCase(String str) throws RemoteException {
        if (str != null) {
            return str.toUpperCase();
        }
        return null;
    }
};

}

6.3 activity中获取mBinder实例

private MyAIDLService myAIDLService;

private ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        myAIDLService = MyAIDLService.Stub.asInterface(service);
        try {
            String upperStr = myAIDLService.toUpperCase("hello world");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值