我正在尝试获取传出号码,但在oreo之前,代码工作正常,但在带有oreo代码的设备中无法正常工作。
public class IncomingBroadCastReceiver extends BroadcastReceiver {
Context context;
WhatsAppContacts requiredContact;
List<String> messages = new ArrayList<>();
String callerName="";
Realm realm;
String outgoingNumber = "";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("Search","onReceive");
this.context = context;
realm = Realm.getDefaultInstance();
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("Search","above Ringing"+state);
if(state == TelephonyManager.CALL_STATE_RINGING){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number", incomingNumber);
Log.d("Ringing", incomingNumber);
editor.apply();
Log.d("Search","inside call state changed");
initialiseAndStartService();
}
else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
if(number!=null){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number", number);
editor.apply();
Log.d("Search","inside call state changed");
initialiseAndStartService();
}
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
在oreo之前的版本中,它可以正常工作。但是在奥利奥(Oreo)中无法获取传出号码。
最佳答案
奥利奥(Oreo)有一个限制。您不能在清单中注册隐式广播,而NEW_OUTGOING_CALL
是隐式广播。仅当您的应用处于直播状态且用户正在与之交互时,您才能获得该广播作品。
您可以找到更多信息here