1, through the aidl and reflection to realize hanging up the telephone
Specific three steps:
(1) Itelephony.aidl, You must create a new Com.android.internal.telephony package and put it into the Itelephony.aidl file (after the build has Itelephony.java file under Gen, which is the interface generated by Aidl), the file contents are as follows:
Package com.android.internal.telephony;
Interface itelephony{
Boolean Endcall ();
void Answerringingcall ();
}
(2) Add the following method to the desired class, as follows (get an instance of the telephony interface through reflection)
/**
* @param context
* @return
*/
private static Itelephony Getitelephony (context context) {
Telephonymanager Mtelephonymanager = (telephonymanager) context
. Getsystemservice (Telephony_service);
class<telephonymanager> C = telephonymanager.class;
Method getitelephonymethod = null;
try {
Getitelephonymethod = C.getdeclaredmethod ("Getitelephony",
(class[]) null); How to get a claim
Getitelephonymethod.setaccessible (TRUE);
} catch (SecurityException e) {
E.printstacktrace ();
} catch (Nosuchmethodexception e) {
E.printstacktrace ();
}
try {
Itelephony itelephony = (itelephony) Getitelephonymethod.invoke (
Mtelephonymanager, (object[]) null); Get instance
return itelephony;
} catch (Exception e) {
E.printstacktrace ();
}
return itelephony;
}
(3) Call this instance when calling, and then call this Endcall () method.
Mtelephonymanager = (Telephonymanager) This
. Getsystemservice (Telephony_service);
Mtelephonymanager.listen (Phonestatelistener,
Phonestatelistener.listen_call_state);
Phone instances
Phonestatelistener Phonestatelistener = new Phonestatelistener () {
@Override
public void oncallstatechanged (int state, String incomingnumber) {
Switch (state) {
Case telephonymanager.call_state_ringing:
Itelephony = Getitelephony (Getapplicationcontext ()); Get Phone interface
if (itelephony! = null) {
try {
Itelephony.endcall (); Hang up the phone
Toast.maketext (Getapplicationcontext (),
"Endcall" + incomingnumber + "successful!",. Show ();
} catch (RemoteException e) {
E.printstacktrace ();
}
}
Break
Default:
Break
}
}
};
aidl:http://download.csdn.net/detail/ab6326795/7993671
The above method is applicable to the previous version 2.3, more than 2.3 can not be used
2, through the broadcast notification system to answer and hang up
Since Android2.3 increases the limit for Permissionandroid.permission.MODIFY_PHONE_STATE, the ability to invoke itelephone through the reflection mechanism before 2.3 does not apply.
2.3 On the implementation mode:
Public synchronized void Answerringingcall () {
The query system Phoneapp application (Phoneglobals.java) realizes the acceptance and processing of the headset insertion, multimedia buttons and other notifications. There is no special place found, personally, if the system received this broadcast should be able to answer or hang up the operation.
More than 2.3 Execute the following code to implement the automatic answer Intent mintent = new Intent (Intent.action_media_button); Press Volume KeyEvent keyevent = new KeyEvent (Keyevent.action_down, keyevent.keycode_headsethook); Mintent.putextra ("Android.intent.extra.KEY_EVENT", keyevent); Call permission allows the program to make calls and replace the dialer interface of the system Mcontext.sendorderedbroadcast (mintent, "Android.permission.CALL_PRIVILEGED"); Mintent = new Intent (Intent.action_media_button); KeyEvent = new KeyEvent (keyevent.action_up,keyevent.keycode_headsethook); Mintent.putextra ("Android.intent.extra.KEY_EVENT", keyevent); Mcontext.sendorderedbroadcast (mintent, "Android.permission.CALL_PRIVILEGED");
All two require permission
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name = "Android.permission.MODIFY_PHONE_STATE"/> <!--the right to call-- <uses-permission android:name= " Android.permission.CALL_PHONE "/>
Combination of the two methods can make the phone automatically answer and hang up the app
Android phone automatic answer and hang-up detailed