2012-12-08 42 views
0

我創建了一個服務來開始/停止歌曲,現在當我點擊「信息」時,服務星星正確,但我無法阻止它。我應該打算在「public void onClick」中停止服務,但我有一個錯誤。這是停止服務的代碼:stopService(new Intent(this,SobService.class));stopService public void onClick

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

回覆我自己:創建一個停止服務的新方法,並在public void onClick –

回答

1

寫您的Activity類的名字,而不是隻"this"在此聲明

stopService(new Intent(YourActivity.this, SobService.class)); 

唯一"this"可以參考匿名類對象本身不是你的活動對象

+0

謝謝你的回覆! –

相關問題