通过startService()和stopService()启动关闭服务。适用于服务和访问者之间没有交互的情况,如果服务和访问者之间需要方法调用或者传递参数,则需要使用bindService和unbindService()方法启动关闭服务于。
采用Cotext.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法,这个时候访问者和服务会绑定在一起。如果访问者要与服务进行通信,那么,onBind()必须防护Ibinder对象,如果访问者退出了,系统会先调用服务的onUnbind()方法,接着调用onDestroy()方法,如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定。如果访问者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()-->onDestroy()方法。
使用示例:(输入学号对应输出其名字)
界面文件:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/studentno"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:id="@+id/button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
</LinearLayout>
后台服务:
package com.example.studentquery;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class StudentService extends Service{
private String[] names = {"张飞","赵云","关羽"};
private IBinder binder = new StudentBinder();
public String query(int no){
if(no>0 && no<4){
return names[no-1];
}
return null;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
private class StudentBinder extends Binder implements IStudent{
public String queryStudent(int no){
return query(no);
}
}
}
访问者:
package com.example.studentquery;
import android.support.v7.app.ActionBarActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private EditText studentno;
private ServiceConnection conn = new StudentServiceConnection();
private IStudent iStudent;
private TextView textName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentno = (EditText) this.findViewById(R.id.studentno);
Button button = (Button) this.findViewById(R.id.button);
textName = (TextView) this.findViewById(R.id.name);
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) {
iStudent = (IStudent) (service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iStudent = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String no = studentno.getText().toString();
String name = iStudent.queryStudent(Integer.valueOf(no));
textName.setText(name);
}
}
}
通过接口访问私有方法:
package com.example.studentquery;
public interface IStudent {
public String queryStudent(int no);
}
最后注意Service需要在清单文件中进行配置。