Android基础(十) Service 服务
1.什么时Service
Service也是Android的四大组件之一。
Service时一种可以长期在后台运行,没有界面的组件。由其他组件调用开始运行。
2.定义Service
需要定义一个类继承Service,而且要在清单文件AndroidManifest.xml定义<service>节点;如果需要使用隐式意图启动,还需要配置<intent-filter>和<action>。
3.启动和停止Service
调用startService()方法,并传入一个Intent对象可以启动Service。Service启动时会调用onCreate()和onStart()方法,而且onCreate()方法仅在第一次调用时执行。
调用stopService()并传入一个Intent对象可以停止Service,停止时会调用onDestroy()方法。
在Activity中也可以使用bindService(Intent intent)来开启一个服务,但是该Service会随着Activity的销毁而销毁。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="开启服务" />
<Button
android:id="@+id/stopBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="停止服务" />
</LinearLayout>public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent intent = new Intent(this, MyService.class);
switch (view.getId()) {
case R.id.startBT:
startService(intent);
break;
case R.id.stopBT:
stopService(intent);
break;
}
}
}public class MyService extends Service {
private boolean isRunning;
@Override
public void onCreate() {// 创建时执行
super.onCreate();
System.out.println("onCreate");
}
@Override
public void onStart(Intent intent, int startId) {// 启动时执行
super.onStart(intent, startId);
System.out.println("onStart");
isRunning = true;
new Thread() {
public void run() {
for (int i = 0; isRunning; i++) {
System.out.println(Thread.currentThread().getName() + ":"
+ i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
@Override
public void onDestroy() {// 停止时执行
super.onDestroy();
System.out.println("onDestroy");
isRunning = false;
}
@Override
public IBinder onBind(Intent intent) {// 绑定服务时执行
System.out.println("onBind");
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
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=".MyService"/>
</application>
</manifest>4.绑定本地Service
使用bindService()方法来绑定服务,需要传入一个自定义的ServiceConnection用来接收IBinder
定义一个业务接口,其中定义需要使用的方法
Service中有一个IBinder继承Binder并实现业务接口,在onBind方法中返回
调用端将IBinder转为接口类型,调用接口中的方法即可调用到服务中的方法。
Activity调用Service中的方法示例:
Activity通过接口调用Service中的方法示例:
5.绑定远程Service(AIDL:Android Interface Definition Language)
远程绑定服务时无法通过同一个接口来调用方法,这时就要使用AIDL技术
将接口的扩展名改为.aidl,去掉所有的权限修饰符
gen文件夹下会自动生成同名的接口
将Service中自定义的IBinder类改为继承接口中的Stub
ServiceConnection中返回的IBinder是代理对象,不能使用强制转换,而是要改用Stub.asInterface()



浙公网安备 33010602011771号