下面我想简单的服务程序。启动服务工作正常,并产生吐司,但停止服务没有。这个简单的服务的code是如下:
公共类MailService延伸服务{
@覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
}
公共无效的onCreate(){
super.onCreate();
Toast.makeText(本,服务启动,Toast.LENGTH_SHORT).show();
}
公共无效onDestroyed(){
Toast.makeText(本,服务毁,Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
这是在哪里这个服务被称为活动的code是如下:
公共类ServiceTest延伸活动{
私人键式启动,停止;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.service_test);
启动=(按钮)findViewById(R.id.btnStart);
停止=(按钮)findViewById(R.id.btnStop);
start.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
startService(新意图(ServiceTest.this,MailService.class));
}
});
stop.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
stopService(新意图(ServiceTest.this,MailService.class));
}
});
}
}
帮我停止服务与停止按钮产生土司在的onDestroy()方法。我已经看到这里就停止服务的问题很多帖子,但不理想所以在发布新的问题。希望能为满意的答复。
解决方案 onDestroyed()
是错误的名称
的onDestroy()
难道你犯了一个错误,只有在这个问题或者您的code吗?
Here I tried simple service program. Start service works fine and generates Toast but stop service does not. The code of this simple service is as below:
public class MailService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
public void onDestroyed(){
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
}
The code of the Activity from where this Service is called is as below:
public class ServiceTest extends Activity{
private Button start,stop;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_test);
start=(Button)findViewById(R.id.btnStart);
stop=(Button)findViewById(R.id.btnStop);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(ServiceTest.this,MailService.class));
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(new Intent(ServiceTest.this,MailService.class));
}
});
}
}
Help me to stop service with that stop button which generates toast in the onDestroy() method. I have already seen many posts regarding stop service problem here, but not satisfactory so posting new question. Hope for satisfactory answer.
解决方案onDestroyed()
is wrong name for
onDestroy()
Did you make a mistake only in this question or in your code too?