这里我试了一下简单的服务程序。启动服务工作正常,并生成吐司,但停止服务不。这个简单的服务的代码如下:在android中停止服务
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();
}
}
从那里该服务被称为活动的代码如下:
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()方法中敬酒。我已经在这里看到很多关于停止服务问题的帖子,但是并不满意,因此发布了新的问题。希望得到满意的答复。
'stopService(serviceIntent)'不起作用吗? – 2011-04-05 17:43:21
*“另外,请记住,被销毁的服务的确切时间取决于Android,并且可能不会立即生效。”*来自:http://stackoverflow.com/questions/2176375/android-service-wont-stop/2176415#2176415 – bigstones 2011-04-05 17:47:07
@chris:我认为stopService(serviceIntent)方法,因为我实施上面没有工作,因为在onDestroy()吐司没有发生停止按钮单击。 – 2011-04-06 07:09:42