我创建了一个启动/停止歌曲的服务,现在当我点击“信息”时,服务明星正确但我无法阻止它。我应该在“public void onClick”中提出停止服务的意图,但我有一个错误。
这是停止服务的代码:stopService(new Intent(this,SobService.class));
private void Info(){
startService(new Intent(this, SobService.class));
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.info, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view).create();
TextView text=(TextView) findViewById(R.id.infoView1);
builder.setCancelable(false);
builder.setPositiveButton("Chiudi", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
//stopService(new Intent(this, SobService.class));
dialog.cancel();
}
});
builder.show();
答案 0 :(得分:1)
在此声明中编写您的活动类名称,而不只是 "this"
stopService(new Intent(YourActivity.this, SobService.class));
唯一 "this"
可以引用匿名类对象本身而不是您的活动对象