0
我正在研究在後臺運行並偵聽傳入呼叫的應用程序。未從服務調用Android PhoneStateListener onPhoneStateChanged方法
爲此,我創建了一個調用onHandleIntent方法中的TelephonyManager.listen的服務。
不幸的是,雖然phonestatelistener的構造函數被調用,但它的onPhoneStateChanged方法沒有被調用。
從活動中進行相同的調用可以正常工作。我很困惑這個問題可能是什麼。我搜查了很多類似的帖子,但沒有一個能令人滿意地回答我的問題。因此我發佈這個問題。
以下是我的服務和phonelistener實現:
public class PhoneListenersService extends IntentService{
TelephonyManager tm;
CallStateListener callstatelistener;
public PhoneListenersService() {
super("PhoneListenersService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
int count=0;
do
{
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.d("Count", ""+count++);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(count<100);
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
請幫助我。
謝謝!
有什麼建議嗎? – user1122549
IntentService如何被解僱?在哪個事件? –
@MadhurAhuja您使用StartService方法調用IntentService。 startService可以通過點擊按鈕或類似的觸發器來調用。 – user1122549