如何限制BroadcastReceiver onReceive仅为传入调用调用

时间:2012-12-06 07:17:47

标签: android broadcastreceiver

我有一个应用程序可以监听来电,并根据号码向该号码发送短信。一切正在进行,onReceive被呼叫并发送短信但是一旦呼叫被拒绝,呼叫或呼叫方onReceive将被再次呼叫,并再次发送短信。我如何限制这一点,所以当收到呼叫时,只会调用onReceive

以下是我的代码的简单版本:

我的BroadcastReceiver

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log;

public class Telephone extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        Log.d("onReceive", "Got Call Signal");

    }

}

in Manifest:

         <receiver android:name="com.example.testbroadcastreceiver.Telephone" >
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

使用许可:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

2 个答案:

答案 0 :(得分:0)

你可以像这样逻辑地处理它:

public class ServiceReceiver extends BroadcastReceiver {

  @Override    
  public void onReceive(Context context, Intent intent) {
    MyPhoneStateListener phoneListener=new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
  }
}

public class MyPhoneStateListener extends PhoneStateListener {
  public void onCallStateChanged(int state,String incomingNumber){
  switch(state){
    case TelephonyManager.CALL_STATE_IDLE:
      Log.d("DEBUG", "IDLE");
    break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
      Log.d("DEBUG", "OFFHOOK");
    break;
    case TelephonyManager.CALL_STATE_RINGING:
      Log.d("DEBUG", "RINGING");
//place your sms code here
    break;
    }
  } 
}

答案 1 :(得分:0)

您需要在BroadcastReceiver内注册PhoneStateListener.LISTEN_CALL_STATE仅接收来电:

public class Telephone extends BroadcastReceiver {

    private Context mContext;
    private Intent mIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;
        TelephonyManager tm = (TelephonyManager)              
                 context.getSystemService(Context.TELEPHONY_SERVICE);
        int events = PhoneStateListener.LISTEN_CALL_STATE;
        tm.listen(phoneStateListener, events);
    }

    private final PhoneStateListener phoneStateListener = 
                                                new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            String callState = "UNKNOWN";
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:

                break;
            case TelephonyManager.CALL_STATE_RINGING:
                                // DO YOUR WORK HERE
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                          if(incomingNumber.length() >0){

                                  // incoming call
                                 // DO YOUR WORK HERE when call is received
                                 // SEND SMS HERE
                            }
                           else{
                                      // outgoing call
                              }
                break;
            }

            super.onCallStateChanged(state, incomingNumber);
        }
    };

}