I'm developing an app where the app has OTP phone call for setting the user pin. So what i'm going to is to set automaticly the phone number i got from phone call to textview. The problem is PhoneStateReceiver class which extend BroadcastReceiver not working on Pie but working on Oreo.
It's work perfectly fine on my Oreo phone but not with Pie phone, there is no error i got, except on Pie the onReceive at PhoneStateReceiver is not called.
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if((state.equals(TelephonyManager.EXTRA_STATE_RINGING))){
Toast.makeText(context,"Received State",Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Intent local = new Intent();
local.setAction("service.to.activity.transfer");
local.setAction("android.intent.action.PHONE_STATE");
local.setAction("android.intent.action.NEW_OUTGOING_CALL");
local.putExtra("number", incomingNumber);
context.sendBroadcast(local);
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Intent local = new Intent();
local.setAction("service.to.activity.transfer");
local.setAction("android.intent.action.PHONE_STATE");
local.setAction("android.intent.action.NEW_OUTGOING_CALL");
local.putExtra("number", incomingNumber);
context.sendBroadcast(local);
}
}
}
}
This is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.jpx.qur">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:name=".modules.App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher_square"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
<receiver android:name=".utils.phoneutil.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
This is how i handle the register receiver on my Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
getNumberPhone();
}
public void getNumberPhone(){
IntentFilter filter = new IntentFilter();
filter.addAction("service.to.activity.transfer");
BroadcastReceiver updateUIReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//UI update here
if (intent != null){
String message = intent.getStringExtra("number");
if(message != null){
if(message.length() == DIGITLENGTH){
String asubstring = message.substring(1, message.length());
pin1.setText(""+asubstring.charAt(0));
pin2.setText(""+asubstring.charAt(1));
pin3.setText(""+asubstring.charAt(2));
pin4.setText(""+asubstring.charAt(3));
}
}
}
}
};
registerReceiver(updateUIReciver, filter);
}
It's work perfectly fine on my Oreo phone but not with Pie phone, there is no error i got, except on Pie the onReceive at PhoneStateReceiver is not called. Please help me to fix this