0

我有一个BroadcastReceiver监听电话,当它们结束时AlertDialog出现。在 Android 5.1 及更低版本上运行良好,但在 Android 6.0 (Marshmallow) 及更高版本上,当有人打电话时,通话后对话框不会出现。它显示何时再次手动启动应用程序以及是否有多个呼叫 - 所有应该立即显示的对话框都立即显示,这是不好的。

有谁知道这个问题是关于什么的?

代码:

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "PhoneStateBroadcast";
Context mContext;
private static String incoming_nr;
private static int prev_state;
static CustomPhoneStateListener customPhoneListener;

@Override
public void onReceive(Context context, Intent intent) {
    if (ContextCompat.checkSelfPermission(
            context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
        if(customPhoneListener == null){
            customPhoneListener = new CustomPhoneStateListener();
            telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }


    if(Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())){
        incoming_nr = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    } else {
        Bundle bundle = intent.getExtras();
        String phoneNr = bundle.getString("incoming_number");
        if(phoneNr != null)incoming_nr = phoneNr;
    }

    Log.v(TAG, "phoneNr: " + incoming_nr);
    mContext = context;
}

public class CustomPhoneStateListener extends PhoneStateListener {

    private static final String TAG = "CustomPhoneStateListnr";

    @Override
    public void onCallStateChanged(int state, String incomingNumber){
        if(incomingNumber != null && incomingNumber.length() > 0){
            incoming_nr = incomingNumber;
        }

        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(TAG, "CALL_STATE_RINGING");
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d(TAG, "CALL_STATE_OFFHOOK");
                prev_state = state;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
                if(prev_state == TelephonyManager.CALL_STATE_OFFHOOK){

                    Realm realm = Realm.getDefaultInstance();
                    Contact contact = realm.where(Contact.class)
                            .contains(Constants.EXTRA_CONTACT_NUMBER, incoming_nr)
                            .findFirst();

                    if(contact != null && !contact.getPopupFlag())
                        break;

                    Intent i = new Intent(mContext, PostCallPromptActivity.class);
                    i.putExtra(Constants.EXTRA_CALL_LOG_NUMBER, incoming_nr);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(i);

                    prev_state = state;
                    //Answered Call which is ended
                }
                if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                    prev_state = state;
                    //Rejected or Missed call
                }
                break;
        }
    }
}

}

显现:

<receiver android:name=".postcall.PhoneStateBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE">
            </action>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>
4

0 回答 0