How to implement Android answer and hang-off

Source: Internet
Author: User

Reference: Android call answering and hang up support for all current versions

Note: The following automatic answer methods are not supported for android2.3 versions and above.

(Will throw exception: Java.lang.SecurityException:Neither user xxxxx Nor current process has android.permission.MODIFY_PHONE_STATE.)

Cause: android2.3 version and above Android.permission.MODIFY_PHONE_STATE permission limit has been changed to system permissions.

Ordinary applications have been unable to invoke, so the online find how to use the Android.permission.MODIFY_PHONE_STATE of the article has been invalidated, but still have to refer to the method is to get your program program System program. One is prefabrication into ROM and the other is using system signatures. The first one I've tried, and the second is yet to be verified.

http://kongweile.iteye.com/blog/1428033

To start with, let's first say how to use the mapping mechanism for automatic answer and hang-off.

The first step: Prepare the System packages and Aidl files required by the application environment.



(1) Create a package in the app: Android.telephony

\framework\telephony\java\android\ the Android system under the frame The Neighboringcellinfo.aidl file in the telephony directory is copied to the package created above (android.telephony);

(2) Create a package in the app: Com.android.internal.telephony

\framework\telephony\java\com\android\internal\ the Android system under the frame The Itelephony.aidl file in the telephony directory is copied to the package created above (com.android.internal.telephony);





Step Two: Create a method to get Itelephony

Phoneutils.java
Java code
Package Com.zhouzijing.android.demo;

Import Java.lang.reflect.Method;
Import Com.android.internal.telephony.ITelephony;
Import Android.telephony.TelephonyManager;

public class Phoneutils {

public static Itelephony Getitelephony (Telephonymanager telephony) throws Exception {
Method Getitelephonymethod = Telephony.getclass (). Getdeclaredmethod ("Getitelephony");
Getitelephonymethod.setaccessible (TRUE);//The privatization function can also be used
Return (Itelephony) Getitelephonymethod.invoke (telephony);
}
}


Step three: Create a phone broadcast interceptor

Myphonebroadcastreceiver.java
Java code
Package Com.zhouzijing.android.demo;

Import Com.android.internal.telephony.ITelephony;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;

public class Myphonebroadcastreceiver extends Broadcastreceiver {

Private final static String TAG = Myphone.tag;

@Override
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
LOG.I (TAG, "[Broadcast]" +action);

Incoming Calls
if (Action.equals (myphone.b_phone_state)) {
LOG.I (TAG, "[broadcast]phone_state");
Doreceivephone (context,intent);
}
}


public void Doreceivephone (context context, Intent Intent) {
String PhoneNumber = Intent.getstringextra (
Telephonymanager.extra_incoming_number);
Telephonymanager telephony = (Telephonymanager) context.getsystemservice (
Context.telephony_service);
int state = Telephony.getcallstate ();

Switch (state) {
Case telephonymanager.call_state_ringing:
LOG.I (TAG, "[broadcast] waiting to pick up the phone =" +phonenumber);
try {
Itelephony itelephony = phoneutils.getitelephony (telephony);
Itelephony.answerringingcall ();//auto-connect phone
Itelephony.endcall ();//auto-hang up the phone
} catch (Exception e) {
LOG.E (TAG, "[broadcast]exception=" +e.getmessage (), E);
}
Break
Case Telephonymanager.call_state_idle:
LOG.I (TAG, "[broadcast] phone hangs up =" +phonenumber);
Break
Case Telephonymanager.call_state_offhook:
LOG.I (TAG, "[broadcast] in call =" +phonenumber);
Break
}
}

}




Fourth: Registration of telephone broadcast interceptors

Myphone.java
Java code
Package Com.zhouzijing.android.demo;

Import android.app.Activity;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;
Import Android.view.View;

public class Myphone extends Activity {
Public final static String TAG = "Myphone";

Public final static String b_phone_state = telephonymanager.action_phone_state_changed;

Private Myphonebroadcastreceiver Mbroadcastreceiver;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.my_phone);
}

Button 1-Registering the broadcast
public void Registerthis (View v) {
LOG.I (TAG, "registerthis");
Mbroadcastreceiver = new Myphonebroadcastreceiver ();
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (b_phone_state);
Intentfilter.setpriority (Integer.max_value);
Registerreceiver (Mbroadcastreceiver, Intentfilter);
}

Button 2-Cancel broadcast
public void Unregisterthis (View v) {
LOG.I (TAG, "unregisterthis");
Unregisterreceiver (Mbroadcastreceiver);
}
}

5th Step: Configure Permissions in Androidmanifest.xml
XML code
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
which
Java code
Itelephony.answerringingcall ();//auto-connect phone

Must have permission Android.permission.MODIFY_PHONE_STATE


Java code
Itelephony.endcall ();//auto-hang up the phone

Must have permission Android.permission.CALL_PHONE

Since Android2.3 increases the limit for permission Android.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.



private void Answerringingcallwithbroadcast (Context context,telephonymanager telmanager) {
Audiomanager Audiomanager = (audiomanager) context.getsystemservice (Context.audio_service);
Determine if the headset is plugged in
if (! Audiomanager.iswiredheadseton ()) {

More than 4.1 systems restrict partial permissions, use the Samsung 4.1 version test tip warning: Permission Denial:not allowed to send broadcast Android.intent.action.HEADSET_PLUG From pid=1324, uid=10017

//You need to be aware that when you send a broadcast with the permission "Android.permission.CALL_PRIVLEGED", you also need to increase that permission when you accept the broadcast. However, more than 4.1 version of this permission can only be obtained by the system application. At the time of testing, the custom receiver could not accept this broadcast, and later removed the permission and set it to null to be heard.


if (Android.os.Build.VERSION.SDK_INT >=15) {
Intent meidabuttonintent = new Intent (Intent.action_media_button);
KeyEvent keyevent = new KeyEvent (keyevent.action_up, Keyevent.keycode_headsethook);
Meidabuttonintent.putextra (intent.extra_key_event,keyevent);
Context.sendorderedbroadcast (meidabuttonintent, NULL);
}else{

The following applies to Android2.3 and over 2.3 versions, but the test finds that the 4.1 system does not work.
Intent localIntent1 = new Intent (intent.action_headset_plug);
Localintent1.addflags (intent.flag_activity_no_history);
Localintent1.putextra ("state", 1);
Localintent1.putextra ("Microphone", 1);
Localintent1.putextra ("name", "Headset");
Context.sendorderedbroadcast (LocalIntent1, "Android.permission.CALL_PRIVILEGED");

Intent LocalIntent2 = new Intent (Intent.action_media_button);
KeyEvent localKeyEvent1 = new KeyEvent (Keyevent.action_down, Keyevent.keycode_headsethook);
Localintent2.putextra (Intent.extra_key_event, localKeyEvent1);
Context. Sendorderedbroadcast (LocalIntent2, "Android.permission.CALL_PRIVILEGED");

Intent LocalIntent3 = new Intent (Intent.action_media_button);
KeyEvent LocalKeyEvent2 = new KeyEvent (keyevent.action_up, Keyevent.keycode_headsethook);
Localintent3.putextra (Intent.extra_key_event, LocalKeyEvent2);
Context.sendorderedbroadcast (LocalIntent3, "Android.permission.CALL_PRIVILEGED");

Intent localIntent4 = new Intent (intent.action_headset_plug);
Localintent4.addflags (intent.flag_activity_no_history);
Localintent4.putextra ("state", 0);
Localintent4.putextra ("Microphone", 1);
Localintent4.putextra ("name", "Headset");
Context.sendorderedbroadcast (LocalIntent4, "Android.permission.CALL_PRIVILEGED");
}

} else {
Intent meidabuttonintent = new Intent (Intent.action_media_button);
KeyEvent keyevent = new KeyEvent (keyevent.action_up, Keyevent.keycode_headsethook);
Meidabuttonintent.putextra (intent.extra_key_event,keyevent);
Context.sendorderedbroadcast (meidabuttonintent, NULL);
}
}

How to implement Android answer and hang-off

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.