Android PhoneStateListener onPhoneStateChanged方法未从服务调用

时间:2014-01-27 17:37:27

标签: android android-service phone-state-listener

我正在开发一个在后台运行并侦听来电的应用。

为此,我创建了一个在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;
           }
          }

         }

}

请帮帮我。

谢谢!

1 个答案:

答案 0 :(得分:0)

没有选择,我改变了我的做法。我使用带有意向服务的广播接收器来监听来电。以下是代码,如果它可以帮助某人: -

public class PhoneListenersService extends IntentService{

    TelephonyManager tm; 
    BroadcastReceiver receiver; 
    IntentFilter filter;

    public PhoneListenersService() { 
        super("PhoneListenersService");
        // TODO Auto-generated constructor stub 
    }

    @Override   
protected void onHandleIntent(Intent intent) { 
        // TODO Auto-generated method stub  
            //Intent
 Filter         filter=new IntentFilter();  
    filter.addAction("android.intent.action.PHONE_STATE"); 
        //Create instance of BroadcastReceiver  
    receiver=new BroadcastReceiver()
{       
    @Override       
    public void onReceive(Context arg0, Intent intent) {
                // TODO Auto-generated method stub
                   Bundle bundle = intent.getExtras();
                   if (bundle == null)
                      return;

                   // Incoming call
                   String state = 

bundle.getString(TelephonyManager.EXTRA_STATE);

                   if ((state != null) &&  

(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) 
{
                       phoneNumber = 

bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 

                       //if phoneNumber is not blank get location details

                       if(!phoneNumber.isEmpty())
                       {
                        //Do work here
                       }                       
                   }        
    }            
        };

        registerReceiver(receiver, filter);     
} }

问候。