Up to Android 7.1 it was possible to end an incoming call by using the ITelephony.endCall()
method and giving your app the permissions android.permission.CALL_PHONE
and android.permission.READ_PHONE_STATE
.
When doing the same on Android 8.0 Oreo (API 26)
, i get this error
12-09 18:11:25.195 16833-16833/li.doerf.leavemealone
E/TelephonyServiceCallHangup: Missing permission MODIFY_PHONE_STATE,
cannot hangup call
Since MODIFY_PHONE_STATE
is a protected permission, my app cannot get it. Is there a way to programmatically end an incoming call on Android 8.0+
?
Please check below 2 link which is helpful to you:
Permission is only granted to system app, in Manifest
https://source.android.com/devices/tech/config/perms-whitelist
above link is used for white-list your MODIFY_PHONE_STATE permission.
This is for security purpose so if you want to access this kind of permission
at that time to white-list your permission after you will call your operations.
I think as per android oreo latest update you can receive phone call in your activity using inbuilt method implementation but those are not providing developer to handle end of call.
Also get(read) phone number of receiving call but you need to define permission of it.
I think my above description is enough to understand
Hope you understand and closed this question.
Try using reflection like in the answer provded here.
It works with READ_PHONE_STATE and CALL_PHONE permissions. Those permissions are available to non-system applications, according to android docs
You need to add this permission in your manifest-:
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
There is a solution that I found. It requires registering as a NotificationListenerService, then parsing the actions and and sending the proper PendingIntents:
public class NotificationListener extends NotificationListenerService {
private static final String TAG = "NotificationListener";
private PendingIntent answerIntent;
private PendingIntent hangupIntent;
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String packageName = sbn.getPackageName();
if ("com.google.android.dialer".equals(packageName)) {
Notification notification = sbn.getNotification();
for (Notification.Action action : notification.actions) {
String title = String.valueOf(action.title);
if ("hang up".equalsIgnoreCase(title) || "decline".equalsIgnoreCase(title)) {
hangupIntent = action.actionIntent;
} else if ("answer".equalsIgnoreCase(title)) {
answerIntent = action.actionIntent;
}
}
}
}
@Override
public void onListenerConnected() {
Log.i(TAG, "Listener connected");
for (StatusBarNotification sbn : getActiveNotifications()) {
onNotificationPosted(sbn);
}
}
public void answerCall() {
try {
answerIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
public void endCall() {
try {
hangupIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
Then declare the following in AndroidManifest.xml:
<service
android:name=".NotificationListener"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
There is one more step: The user has to manually enable your app as a notification listener. You can present a dialog with instructions, then send the user to the settings page using this code:
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
startActivity(intent);