Lollipop : Accept incoming call Programatically in android lollipop - android

I'm using the below code to answer an incoming call from my app(BroadcastReceiver's onReceive()) , it is working in Kitkat . The same code is not working in Lollipop.
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
// send ordered broadcast
context.sendOrderedBroadcast(intent, null);
Please let me know how can I answer a call in Lollipop.
Thank you.

This worked for me.Put this code in your broadcast reciever for action "android.intent.action.PHONE_STATE" .Your phone needs to be rooted.Generate an apk file of ur app and put it into /system/priv-apps/ .Works for Android v 5.0 i.e lollipop.
final String LOG_TAG = "TelephonyAnswer";
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
if (tm == null) {
// this will be easier for debugging later on
throw new NullPointerException("tm == null");
}
// do reflection magic
tm.getClass().getMethod("answerRingingCall").invoke(tm);
} catch (Exception e) {
}
Do not forget to add permission in ur manifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
if modify_phone_state doesnt work explicitly ask for one by using this code
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.MODIFY_PHONE_STATE},
1);
and overide method
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(MainActivity.this, "Permission denied to pick call", Toast.LENGTH_SHORT).show();
}
return;
}
}
}

From Lollipop, if our application is System app or application has root access then only we can programmatically answer incoming call
For 3rd party developer application, Lollipop OS do not allow to
answer programmatically to incoming call
You can check if how to do it for system app or with root access : In this Answer

Related

android airplane permission doesn't work properly - full example?

I've found a lot of examples here but no one works for me...I can't understand what's wrong.what I need is to enter the "airplane mode" in my app and I search for a full example.
What I have done is
1. request permission in manifest (I've seen I need "WRITE_SETTINGS" permission)
<uses-permission-sdk-m android:name="android.permission.WRITE_SETTINGS" />
2. in the code, when I want to change Airplane Mode, I check for permission
private void setAirplaneMode() {
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(this);
} else {
permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
//here I go to activate the airplane mode
setAirplaneModeIntent(true);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_SETTINGS)) {
//I need to explain why I want permission
//TODO
} else {
ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.WRITE_SETTINGS}, MY_PERMISSION_REQUEST_WRITE_SETTINGS);
}
}
3. I override the "onRequestPermissionsResult"
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResult) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_WRITE_SETTINGS: {
if( grantResult.length > 0 &&
grantResult[0] == PackageManager.PERMISSION_GRANTED) {
setAirplaneModeIntent(true);
}
}
}
}
4. in the "setAirplaneModeIntent" I set the Airplane mode
private void setAirplaneModeIntent(boolean activateAirplaneMode) {
Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
activateAirplaneMode ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", activateAirplaneMode);
sendBroadcast(intent);
}
when I debug, I enter in the "ActivityCompat.requestPermissions" code but when I reach the "onRequestPermissionsResult" the result is not PackageManager.PERMISSION_GRANTED.
What's wrong?
EDIT
I solve the "requestPermissions" part. For the "WRITE_SETTINGS" permission you need the ACTION_MANAGE_WRITE_SETTINGS intent.
See here:
https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_WRITE_SETTINGS
https://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS
http://stackoverflow.com/questions/39224303/write-settings-permission-not-granted-marshmallow-android
Anyway I got an error in the point 4, when I send the broadcast.
If I don't sent the broadcast, the "Settings.System.putInt" command seems doing nothing.
Someone can help me?
EDIT 2
I surrender. The only solution I have found that works is to open the network settings in this way
Intent intentAirplaneMode = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);
intentAirplaneMode.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentAirplaneMode);

Android 6.0 - Bluetooth - No code exists for Action_Found broadcast intent

UPDATE
I tried many codes, also from examples shown on the internet. each of them follows my approach. After many hours of testing, i came to the conclusion that on Android 6.0 there's no chance to achieve bluetooth discovery of unknown devices, we can only retrieve the bonded ones.
I'm pretty sure there's something with this android version.
if someone knows how to fix this, i would really appreciate any help.
Original Post
My code is working fine, but no devices get found.
i only receive DISCOVERY_STARTED and DISCOVERY_FINISHED, so no devices are found, but using system app these devices get found.
This is the code of my application, hope it can help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
//other stuff...
IntentFilter filter=new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(myreceiver,filter);
}
final BroadcastReceiver myreceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("test","RECEIVED: "+ action);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
}
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("test", device.getName() + "\n" + device.getAddress());
}
}};
public void scanDevices(View v){
if (bluetoothAdapter.isEnabled()){
bluetoothAdapter.startDiscovery();
}
}
I already got permissions set:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Very simple solution:
1. Add FINE_LOCATION permission to manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
2. Request FINE_LOCATION permission at runtime:
//since i was working with appcompat, i used ActivityCompat method, but this method can be called easily from Activity subclassess.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);
3. Implement onRequestPermissionsResult method:
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_CONSTANT: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted!
}
return;
}
}
}
All this because Marshmallows requires this permission in order to use bluetooth for discovery.
Since this permission belongs to the Dangerous group of permissions, simply declaring it in the manifest doesn't work, we need the user's explicit consent to use the position (even if we don't need the position actually).

Android permissions READ_CONTACT [duplicate]

This question already has answers here:
Android permission doesn't work even if I have declared it
(11 answers)
Closed 6 years ago.
Why i get this message
if the i set the permission in the manifest file:
Thank you for any help.
You need request permission at runtime in android >= 6.0
https://developer.android.com/training/permissions/requesting.html
You have to grant permission in android version >=6.0 (Marshmallow and above).
For Android 6.0+, you have to request the permission at runtime as well as adding it to the manifest. So something like this added to your Activity onCreate();
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.READ_CONTACTS);
//...add any others you need to this arraylist
// Convert the List to an Array
String[] permissionsArray = permissions.toArray(new String[0]);
ActivityCompat.requestPermissions(getActivity(), permissionsArray, REQUEST_CODE);
//The REQUEST_CODE is just a integer that your callback method will check, set it to some number
Then override this method in your activity to check whether the request was granted or not
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted, do your thing
} else {
//Permission denied, you can't do what you want to because user didn't give you permission
}
return;
}
}
See here for more info https://developer.android.com/training/permissions/requesting.html
Change your target sdk version to 22 in your app's build.gradle if you donot want to request the permission to the user at run time.
Else if you want to keep the target sdk version to 23 then you would need to request the permission from the user at run time as mentioned above by #wanpanman
Call this permission.
//Check permissions
public boolean reqPermission() {
if (!Permissions.checkPermission(this, Manifest.permission.READ_CONTACTS)) {
if (shouldShowRationale(Manifest.permission.READ_CONTACTS)) {
Snackbar.make(yourLayout, "Need to enable contact permission to use this feature. bla bla bla",
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.settings, new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
})
.show();
return true;
} else {
Permissions.grantPermission(this, PERMISSION_REQUEST_CODE);
return true;
}
} else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.M)
public boolean shouldShowRationale(String permission) {
return shouldShowRequestPermissionRationale(permission);
}

Android 6 bluetooth

I upgraded to Android 6 and my applications who use Bluetooth doesn't work with this new API version. It's the same problem with application on Play Store: Bluetooth spp tools pro (good application to view if bluetooth works) which doesn't discovery of devices.
The problem seems to be in Bluetooth discovery:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery()
Log.i("BLUETOOTH", String.valueOf(mBluetoothAdapter.isDiscovering())); // Return false
My applications work well with Android 4/5 and I followed : http://developer.android.com/guide/topics/connectivity/bluetooth.html
Staring with Android 6.0 it is not enough to include permissions on manifest.
You have to ask the user explicitly about each permission that is considered "dangerous".
BluetoothDevice.ACTION_FOUND requires BLUETOOTH and ACCESS_COARSE_LOCATION permissions
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_FOUND
The ACCESS_COARSE_LOCATION
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION
is a "dangerous" permission and therefore you have to ask for it using requestPermission before doing actual discovery.
public void doDiscovery() {
int hasPermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (hasPermission == PackageManager.PERMISSION_GRANTED) {
continueDoDiscovery();
return;
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
android.Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_COARSE_LOCATION_PERMISSIONS);
}
then on you will get the user answer on onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_COARSE_LOCATION_PERMISSIONS: {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
continueDoDiscovery();
} else {
Toast.makeText(this,
getResources().getString(R.string.permission_failure),
Toast.LENGTH_LONG).show();
cancelOperation();
}
return;
}
}
}
To work with previous versions of android you should use compatibility libraries and make the calls using ActivityCompat
I've spent some time investigating the problem.
Created bug report on Android bug tracker here
The problem is that system does not forward BluetoothDevice.ACTION_FOUND intents to the registered BroadcastReceiver. Logcat shows lines like this:
10-16 07:34:09.147 786-802/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } to ProcessRecord{5ce2d92 21736:com.example.mvl.bluetoothtest/u0a74} (pid=21736, uid=10074) requires android.permission.ACCESS_COARSE_LOCATION due to sender com.android.bluetooth (uid 1002)
Which themes for me that the application needs android.permission.ACCESS_COARSE_LOCATION permission to receive this intents. i personaly don't understand why I need that permission to get the Bluetooth devices around.
So if you add this permission to you Manifest, then it should work with one more precondition - You have to set target SDK and compile with SDK not higher, then 22.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
When checking the source code in GattService.java,you will find some code comments in method onScanResult:
// Do no report if location mode is OFF or the client has no location permission
// PEERS_MAC_ADDRESS permission holders always get results
if (hasScanResultPermission(client) && matchesFilters(client, result)) {
try {
ScanSettings settings = client.settings;
if ((settings.getCallbackType() &
ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
app.callback.onScanResult(result);
}
} catch (RemoteException e) {
Log.e(TAG, "Exception: " + e);
mClientMap.remove(client.clientIf);
mScanManager.stopScan(client);
}
}
this clarified what is needed to get a Bluetooth LE advertising report.

End call in android programmatically

I see a lot of questions that it's impossible to end call programmatically in Android.
At the same time, I see a lot of dialer apps in googleplay market where you can activate the call and drop it also. How do they work?
Edit: I've read somewhere that my app has to be system app. Then how to make it system, and what is the difference between system and user apps?
You do not need to be a system app. First, create package com.android.internal.telephony in your project, and put this in a file called "ITelephony.aidl":
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
Once you have that, you can use this code to end a call:
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.endCall();
You could use this inside a PhoneStateListener, for example. For this to work, you require permissions in manifest:
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Edit: Apologies for horrible formatting, I still can't figure out how to properly do code blocks here :/
For Android P (since Beta 2) and above, there is finally a formal API for endCall:
https://developer.android.com/reference/android/telecom/TelecomManager#endCall()
The ANSWER_PHONE_CALLS permission is required in manifest:
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
With the permission, for API level 28 or above:
TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
if (tm != null) {
boolean success = tm.endCall();
// success == true if call was terminated.
}
At the same time the original endCall() method under TelephonyManager is now protected by MODIFY_PHONE_STATE permission, and can no longer be invoked by non-system Apps by reflection without the permission (otherwise a Security Exception will be triggered).
For Information.
May be of use in some situations. There is a potential workaround using the InCallService class. Most of the required information is here.https://developer.android.com/reference/android/telecom/InCallService.html#onCallRemoved(android.telecom.Call)
It does require setting your app as the default phone app and ensuring the following is granted.
<uses-permission android:name="android.permission.CALL_PHONE" />
If you implement your own class extending InCallService then when a call starts the call binds to your app and you get the call information through the onCallAdded() function. You can then simply call.disconnect() and the call will end.
Cut Call for the Api 28+
private void cutCall(){
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_PHONE_STATE }, PHONE_STATE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == PHONE_STATE) {
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ANSWER_PHONE_CALLS }, ANSWER_CALLS);
} else if (requestCode == ANSWER_CALLS) {
cutTheCall;
}
}
}
//This code will work on Android N (Api 28 and Above)
private boolean cutTheCall() {
TelecomManager telecomManager = (TelecomManager) getApplicationContext().getSystemService(TELECOM_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || telecomManager == null) {
return false;
}
if (telecomManager.isInCall()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
callDisconnected = telecomManager.endCall();
}
}
return true;
}
SilenceRinger() does not work for android 2.3+ versions. Just comment it, other code will work fine.
Hope this works for you!
Along with Adding android telephony interface and a broadcast receiver, you will also need to add android manifest receiver entry with the action android.intent.action.PHONE_STATE for the reciever you want to handle intent.
You will get compile time error if you add
uses-permission android:name="android.permission.MODIFY_PHONE_STATE`
in your manifest file. But even if we remove this, it automatically rejects the incoming calls.
public static boolean isCallActive(Context context){
AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(manager.getMode()==AudioManager.MODE_IN_CALL || manager.getMode()==AudioManager.MODE_IN_COMMUNICATION){
return true;
}
return false;
}
Just to add to #headuck's answer. For API 28, you also need to add:
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
then request the permission in your activity. In total I requested these permissions to make it work (READ_PHONE_STATE, CALL_PHONE, ANSWER_PHONE_CALLS, READ_CONTACTS, READ_CALL_LOG)
You can end calls using Telecom manager. I tested it and it worked.
You need permission ANSWER_PHONE_CALLS to do so. Even though it hints to answered calls I had it ending a call made from this phone. And this works for modern Androids.
TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
telecomManager.endCall();

Categories

Resources