2012-12-31 31 views
1

我有一個onClickListener來創建一些按鈕,然後我想停止這個活動,但是因爲我在一個界面上,所以無法獲得活動Intent。從接口添加一個活動

那怎麼看起來像:

 b1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) 
     { 
      //blah blah blah 
      this.stopService(this.getIntent()); // <-- impossiblee, compliation error. 
     } 
    }); 
+0

'this.stopService(getIntent());'或'this.stopService(YourActivityName.this.getIntent());' – Houcine

+0

方法stopService是不是一個部分OnClickListener接口。 –

+0

對不起,試試這個: 'stopService(getIntent());'或'YourActivityName.this.stopService(YourActivityName.this.getIntent());' – Houcine

回答

1

對於停止活動,您將需要使用活動的finish()方法

b1.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) 
    { 
     //blah blah blah 
     Current_Activity.this.finish(); 
    } 
    }); 

和用於停止的,而不是活動服務stopService服務方法

編輯:如果你想停止服務,然後使用

Current_Activity.this.stopService(Current_Activity.this.getIntent()); 
+0

感謝它的工作。 –

3

使用

MyActivity.this.getIntent() 
+0

你可以發佈修改後的代碼嗎?同時發佈代碼所在的位置,它是否創建一個活動? – yoah

+0

我刪除了我的評論,因爲我犯了一個錯誤,你的回答是正確的。如果我可以標記2個答案是正確的,我會。謝謝。 –

相關問題