The above approach works. I just want to add runtime permission for CALL_PHONE for Android + versions.
Follow the inline directions:
Define permission in Android manifest file.
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Now get launch permission for Android + version.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_ACCESS_CALL_PHONE); } else { // Proceed as we already have permission. } } else { // Proceed as we need not get the permission } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case PERMISSION_ACCESS_CALL_PHONE: if (grantResults.length > 0 && grantResults[0] PackageManager.PERMISSION_GRANTED) { // All good! //Toast.makeText(context, "All Good", Toast.LENGTH_SHORT).show(); } else { finish(); //Toast.makeText(this, "Need call phone permission", Toast.LENGTH_SHORT).show(); } break; } }
Now use the built-in method to automatically scroll through the call.
private void declinePhone(Context context) throws Exception { try { String serviceManagerName = "android.os.ServiceManager"; String serviceManagerNativeName = "android.os.ServiceManagerNative"; String telephonyName = "com.android.internal.telephony.ITelephony"; Class<?> telephonyClass; Class<?> telephonyStubClass; Class<?> serviceManagerClass; Class<?> serviceManagerNativeClass; Method telephonyEndCall; Object telephonyObject; Object serviceManagerObject; telephonyClass = Class.forName(telephonyName); telephonyStubClass = telephonyClass.getClasses()[0]; serviceManagerClass = Class.forName(serviceManagerName); serviceManagerNativeClass = Class.forName(serviceManagerNativeName); Method getService = // getDefaults[29]; serviceManagerClass.getMethod("getService", String.class); Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class); Binder tmpBinder = new Binder(); tmpBinder.attachInterface(null, "fake"); serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder); IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone"); Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class); telephonyObject = serviceMethod.invoke(null, retbinder); telephonyEndCall = telephonyClass.getMethod("endCall"); telephonyEndCall.invoke(telephonyObject); } catch (Exception e) { e.printStackTrace(); Log.d("unable", "msg cant dissconect call...."); }
What is it! You will go well.
Hurrah!
AndroidHacker 04 Oct '16 at 7:22 2016-10-04 07:22
source share