So I'm trying to get Incoming Calls and SMS details in my app. I have tried the code shown below and it works on Oneplus 6t (android 10), Moto g plus (android 8), and Samsung M51 (android 10) and it is working as intended. but when used on Redmi Note 3 (android 6.0.1) the SMS part works but the incoming call part is not working. I have tried to look up on google and Stack Overflow but nothing has fixed the issue yet.
AndroidManifest.xml
Permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
Receiver
<receiver android:name="com.earthenergy.earthenergyev.AnswerCallBroadcastReceiver" android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
BroadcastReceiver Class
public class AnswerCallBroadcastReceiver extends BroadcastReceiver {
static PhonecallStartEndDetector listener;
String contactName;
Context context;
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private SmsMessage getIncomingMessage(Object aObject, Bundle bundle) {
SmsMessage currentSMS;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
currentSMS = SmsMessage.createFromPdu((byte[]) aObject, format);
} else {
currentSMS = SmsMessage.createFromPdu((byte[]) aObject);
}
return currentSMS;
}
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
if (listener == null) {
listener = new PhonecallStartEndDetector();
}
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
listener.setOutgoingNumber(intent.getExtras().getString(
"android.intent.extra.PHONE_NUMBER"));
return;
}
if(intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage currentSms;
for(Object aObject: pdus) {
currentSms = getIncomingMessage(aObject, bundle);
String sender = currentSms.getDisplayOriginatingAddress();
String message = currentSms.getDisplayMessageBody();
Log.d("============","Message: "+ message);
Log.d("============","Sender: "+ sender);
Toast.makeText(context,"Number: "+sender+"\nMessage: "+message,Toast.LENGTH_SHORT).show();
}
}
return;
}
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class PhonecallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
boolean isIncomingPicked;
boolean isOutgoingStarted;
String savedNumber;
public PhonecallStartEndDetector() {
}
public void setOutgoingNumber(String number) {
savedNumber = number;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// state is changed
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
//an incoming call has been started
String name = getContactName(incomingNumber, context);
Toast.makeText(context, "Incoming Number: " + incomingNumber + "\nContact Name: " + name, Toast.LENGTH_SHORT).show();
Log.d("==============", "Number: " + incomingNumber);
Log.d("==============", "Name: " + name);
isIncoming = true;
savedNumber = incomingNumber;
break;
}
}
}
So I'm trying to get Incoming Calls and SMS details in my app. I have tried the code shown below and it works on Oneplus 6t (android 10), Moto g plus (android 8), and Samsung M51 (android 10) and it is working as intended. but when used on Redmi Note 3 (android 6.0.1) the SMS part works but the incoming call part is not working. I have tried to look up on google and Stack Overflow but nothing has fixed the issue yet.
AndroidManifest.xml
Permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
Receiver
<receiver android:name="com.earthenergy.earthenergyev.AnswerCallBroadcastReceiver" android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
BroadcastReceiver Class
public class AnswerCallBroadcastReceiver extends BroadcastReceiver {
static PhonecallStartEndDetector listener;
String contactName;
Context context;
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private SmsMessage getIncomingMessage(Object aObject, Bundle bundle) {
SmsMessage currentSMS;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
currentSMS = SmsMessage.createFromPdu((byte[]) aObject, format);
} else {
currentSMS = SmsMessage.createFromPdu((byte[]) aObject);
}
return currentSMS;
}
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
if (listener == null) {
listener = new PhonecallStartEndDetector();
}
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
listener.setOutgoingNumber(intent.getExtras().getString(
"android.intent.extra.PHONE_NUMBER"));
return;
}
if(intent.getAction().equals(SMS_RECEIVED)) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage currentSms;
for(Object aObject: pdus) {
currentSms = getIncomingMessage(aObject, bundle);
String sender = currentSms.getDisplayOriginatingAddress();
String message = currentSms.getDisplayMessageBody();
Log.d("============","Message: "+ message);
Log.d("============","Sender: "+ sender);
Toast.makeText(context,"Number: "+sender+"\nMessage: "+message,Toast.LENGTH_SHORT).show();
}
}
return;
}
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class PhonecallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
boolean isIncomingPicked;
boolean isOutgoingStarted;
String savedNumber;
public PhonecallStartEndDetector() {
}
public void setOutgoingNumber(String number) {
savedNumber = number;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// state is changed
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
//an incoming call has been started
String name = getContactName(incomingNumber, context);
Toast.makeText(context, "Incoming Number: " + incomingNumber + "\nContact Name: " + name, Toast.LENGTH_SHORT).show();
Log.d("==============", "Number: " + incomingNumber);
Log.d("==============", "Name: " + name);
isIncoming = true;
savedNumber = incomingNumber;
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.