2012-09-25 26 views
0

我有一個phonestatelistener: 我該如何在這裏開始服務? 我想:PhoneStateListener和服務

startService(new Intent(this, TTS.class)); 

它doent怎麼過的,我

private PhoneStateListener mPhoneListener = new PhoneStateListener() { 

      @SuppressLint("SdCardPath") 
     public void onCallStateChanged(int state, String incomingNumber) { 

      try { 
      switch (state) { 
      case TelephonyManager.CALL_STATE_RINGING: 
       //i want to start service 
       startService(new Intent(this, TTS.class)); 
+0

錯誤是什麼? – njzk2

+0

鎖在此:http://stackoverflow.com/questions/9361117/how-to-start-phonestatelistener-programmatically – Pasha

+0

我得到這個錯誤,當我寫我上面寫的行:「構造函數Intent(new PhoneStateListener() {},Class )未定義,@ Pasha,我不明白你的鏈接我應該怎麼做才能啓動服務? –

回答

0

工作,當你調用

startService(new Intent(this, TTS.class)); 

new Intent()的第一個參數必須是一個Context。通常你可以通過ServiceActivity來調用它,它們都是Context

在你的情況,你的this參數是PhoneStateListener類,它不延伸Context。我認爲這是一個私人的內部類。您需要指定這樣的外部類:

startService(new Intent(MyActivity.this, TTS.class)); 

如果你的外部類是一個活動,然後MyActivity應該是活動的名稱。如果外部類是服務,則MyActivity應該是服務的名稱。