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();
}
}
这篇博客展示了如何在Android中通过CheckBox监听事件来启动Service。当CheckBox被选中时,Service会被创建并显示相关信息,如'Service created!'和'Service started by user.'。博客详细地解释了Service的生命周期方法onCreate(), onDestroy()和onStart()。
1365

被折叠的 条评论
为什么被折叠?



