在android中停止服务 [英] stop service in android
问题描述
这里我尝试了简单的服务程序.启动服务工作正常并生成 Toast 但停止服务不会.这个简单服务的代码如下:
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();
}
}
调用此Service的Activity代码如下:
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));
}
});
}
}
使用在 onDestroy() 方法中生成 toast 的停止按钮帮助我停止服务.我已经在这里看到了很多关于停止服务问题的帖子,但并不令人满意,所以发布了新问题.希望得到满意答复.
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()
是错误的名称
onDestroy()
你是只在这个问题上还是在你的代码中犯了错误?
Did you make a mistake only in this question or in your code too?
这篇关于在android中停止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!