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 :/
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.
SilenceRinger()
does not work for android 2.3+ versions. Just comment it, other code will work fine. Hope this works for you!
上一篇: C中的反射支持
下一篇: 以编程方式在android中结束调用