呼び出しを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>