通过startService()和stopService()启动关闭服务。适用于服务和访问这之间没有交互的情况。如果服务和访问者之间需要方法调用或者传递参数,则需要用bindService()和unbindService()方法启动关闭服务。
采用Context.bindService()方法启动服务,在服务未被创建时,系统会调用服务的onCreate()方法,接着调用onBind()方法,这个时候访问者和服务绑定在一起。如果访问者要与服务进行通信。那么,onBind()方法必须返回Ibinder。如果访问者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用)如果访问者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()-->onDestroy()方法。
示例代码:
1.MainActivity.java
<span style="font-size:18px;">package com.example.student;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText studentno;
private Button button;
private TextView resultiew;
private IStudent iStudent;
private ServiceConnection conn = new StudentServiceConnection();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentno = (EditText)this.findViewById(R.id.studentno);
resultiew = (TextView)this.findViewById(R.id.resultview);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
Intent service = new Intent(this,StudentService.class);
bindService(service, conn, BIND_AUTO_CREATE);
}
private class StudentServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
iStudent = (IStudent)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
iStudent = null;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unbindService(conn);
super.onDestroy();
}
private final class ButtonClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String no = studentno.getText().toString();
String name = iStudent.queryStudent(Integer.valueOf(no));
resultiew.setText(name);
}
}
}</span>
2.IStudent.java
<span style="font-size:18px;">package com.example.student;
public interface IStudent {
public String queryStudent(int no);
}
</span>
3.StudentService.java
<span style="font-size:18px;">package com.example.student;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class StudentService extends Service {
private String[] name = {"张三","李四","王二"};
private IBinder binder = new StudentBinder();
public String query(int no) {
if(no>0&&no<4) {
return name[no-1];
}
return null;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
private class StudentBinder extends Binder implements IStudent{
@Override
public String queryStudent(int no) {
// TODO Auto-generated method stub
return query(no);
}
}
}
</span>
4.activity_main.xml
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/studentno" />
<EditText
android:id="@+id/studentno"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"/>
<TextView
android:id="@+id/resultview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</span>
5.AndroidManifest.xml在activity节点中添加
<span style="font-size:18px;"><service android:name="com.example.student.StudentService"/></span>
6.strings.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StudentFind</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="studentno">学号</string>
<string name="button">查询</string>
</resources>
</span>