Android 开发教程之 Service 详解
2010-02-22 16:14:00 来源:WEB开发网import android.widget.Toast;
public class TestServiceHolder extends Activity {
private boolean _isBound;
private TestService _boundService;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_service_holder);
setTitle("Service Test");
initButtons;
}
private ServiceConnection _connection = new ServiceConnection {
public void onServiceConnected(ComponentName className, IBinder service) {
_boundService = ((TestService.LocalBinder)service).getService;
Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show;
}
public void onServiceDisconnected(ComponentName className) {
// unexpectedly disconnected,we should never see this happen.
_boundService = null;
Toast.makeText(TestServiceHolder.this, "Service connected",
Toast.LENGTH_SHORT).show;
}
};
private void initButtons {
Button buttonStart = (Button) findViewById(R.id.start_service);
buttonStart.setOnClickListener(new OnClickListener {
public void onClick(View arg0) {
startService;
}
});
Button buttonStop = (Button) findViewById(R.id.stop_service);
buttonStop.setOnClickListener(new OnClickListener {
public void onClick(View arg0) {
stopService;
}
});
Button buttonBind = (Button) findViewById(R.id.bind_service);
buttonBind.setOnClickListener(new OnClickListener {
public void onClick(View arg0) {
bindService;
}
});
Button buttonUnbind = (Button) findViewById(R.id.unbind_service);
buttonUnbind.setOnClickListener(new OnClickListener {
更多精彩
赞助商链接