CheckBox checkBox =
(CheckBox) findViewById(R.id.check_Box);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
startService(new Intent(this,TheService.class));
}
}
});
和服务:
public class TheService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this,"Service created!",Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this,"Service stopped",Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent,int startid) {
Toast.makeText(this,"Service started by user.",Toast.LENGTH_LONG).show();
}
}