Service是android 系统中的一种组件,它跟Activity的级别差不多,但是他不能自己运行,只能后台运行,并且可以和其他组件进行交互。Service的启动有两种方式:context.startService()和context.bindService()。
使用context.startService()
启动Service是会会经历:
context.startService() ->onCreate()- >onStart()->Service
running
context.stopService() | ->onDestroy() ->Service
stop
如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。
stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) -->
onDestroy
使用使用context.bindService()启动Service会经历:
context.bindService()->onCreate()->onBind()->Service
running
onUnbind() ->onDestroy()
->Service stop
onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) -->
onUnbind --> onDestory。
在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
1.在ECLIPSE 下面新建工程 testAidl,建立包名称:com.cardroid.aidl
2.在工程建立完成后:新建文件UnTtleText file名称为:TestAidlInterface然后在里面写上自己的内容
package com.cardroid.aidl;
interface TestAidlInterface
{
void testString(String name);
void testInt(int num);
}
然后保存,保存的时候后缀为.aidl,如果里面接口的内容不写错的话,ADT会帮你在gen下面帮你生成一个和你当前AIDL接口文件所在的包名一样的下面会有一个同名的JAVA文件
这个文件不用我们去管理。
3,然后将接口和SERVICE绑定,我们新建服务类后,在服务类中将接口类实现,返回绑定到服务中:
package com.cardroid.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.cardroid.aidl.TestAidlInterface.Stub;
public class AidlService extends Service{
private String TAG="AidlService";
@Override
public void onCreate() {
Log.i(TAG, "===onCreate=====");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "===onStartCommand=====");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "=======onBind======");
return mBinderStub;
//return new MyServiceImpl();
}
private TestAidlInterface.Stub mBinderStub=new TestAidlInterface.Stub() {
@Override
public void testString(String name) throws RemoteException {
Log.i(TAG, "testString(String name)==="+name);
}
@Override
public void testInt(int num) throws RemoteException {
Log.i(TAG, "testInt()=num=="+num);
}
};
}
4,在客户端实现调用aidl ,常在ACTIVITY中实现调用:
package com.cardroid.aidl;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AidlActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="===AidlActivity===";
private TextView mText;
private TestAidlInterface aidlService=null;
private ServiceConnection mConnection=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "===onServiceConnected======");
aidlService= TestAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "===onServiceDisconnected======");
aidlService=null;
}
};
private void binder(){
ContextWrapper cw = new ContextWrapper(this);
cw.startService(new Intent(cw, AidlService.class));
// cw.bindService((new Intent()).setClass(cw, AidlService.class), mConnection, 0);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent service = new Intent("com.cardroid.aidl.TestAidlInterface");
service.putExtra("name", "aaa");
bindService(service, mConnection, Context.BIND_AUTO_CREATE);
mText=(TextView) findViewById(R.id.tv);
mText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
aidlService.testInt(5);
aidlService.testString("TOM");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
最后在AndroidManifest.xml将服务,ACTIVITY,和AIDL配置好:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cardroid.aidl"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AidlActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AidlService"
><!-- android:process=".remote" -->
<intent-filter>
<action android:name="com.cardroid.aidl.TestAidlInterface"/>
</intent-filter>
</service>
</application>
</manifest>
一个aidl就完成了,注意事项:
在AndroidManifest.xml文件中配置AIDL服务,尤其要注意的是,<action>标签中android:name的属性值就是客户端要引用该服务的ID,也就是Intent类的参数值。
aidl文件的内容与Java代码非常相似,但要注意,不能加修饰符(例如,public、private)、AIDL服务不支持的数据类型(例如,InputStream、OutputStream)等内容。
<!-- android:process=".remote" -->如果服务和本地在一起(即时同一个包名下)就不能添加这个属性,如果在两个不同的工程或者不同的包名下面就要使用这个属性。
aidl文件最好和服务类放入到同一个包名下面,因为是结合服务使用的。