Android call monitoring and power-off monitoring, android call monitoring
I think writing an article is useful, and I must have my own ideas. The following three questions will be taken into consideration about the call-to-power listening process:
1. What is the purpose of listening for power-off calls?
2. How do I listen? Is the method the call-to-power monitoring method the same?
3. What should I pay special attention to in practice?
Monitoring incoming calls for Electric Energy
1. Identify the phone number to be monitored and tell the user that the phone number is fraud, marketing, advertising, or something.
2. Automatic hanging up for special calls to avoid disturbing users
Call/power-off monitoring method (different methods)
1. Call monitoring (PhoneStateListener)
Call listening is usedPhoneStateListenerClass. The method of use is to register the PhoneStateListener object (typically, the PhoneStateListener class inherited by itself to complete some encapsulation) to the system telephone Management Service (TelephonyManager)
Then use the callback method of PhoneStateListenerOnCallStateChanged(Int state, String incomingNumber) for incoming call monitoring (For details, refer to the extended reading section below)
Register a listener
// PhoneServiceName is the service name, generally "phone" --> Context. TELEPHONY_SERVICETelephonyManager telephonyManager = (TelephonyManager) mContext. getSystemService (phoneServiceName); if (telephonyManager! = Null) {try {// register the call listener telephonyManager. listen (mTelephonyListener, PhoneStateListener. LISTEN_CALL_STATE);} catch (Exception e) {// Exception capture }}
The onCallStateChanged method of PhoneStateListener listens to the incoming call status.
@ Overridepublic voidOnCallStateChanged(Int state, String incomingNumber) {switch (state) {case TelephonyManager. CALL_STATE_IDLE: // call up break; case TelephonyManager. CALL_STATE_OFFHOOK: // call the break; case TelephonyManager. CALL_STATE_RINGING: // call through break; default: break ;}}
Three States source code explanation
/** Device call state: No activity. */public static final int CALL_STATE_IDLE = 0; // call/** Device call state: Ringing. A new call arrived and is * ringing or waiting. in the latter case, another call is * already active. */public static final int CALL_STATE_RINGING = 1; // incoming call/** Device call state: Off-hook. at least one call exists * that is dialing, active, or on hold, and no callare ringing * or waiting. */public static final int CALL_STATE_OFFHOOK = 2; // call through
2. Power-off listening (implemented through broadcast)
// OutgoingCallListener inherits a BroadcastReceiver
What do you need to pay special attention?
1. How to obtain dual-card dual-waiting mobile phones
For dual-card mobile phones, each card corresponds to a Service and a FirewallPhoneStateListener. You need to register your own FirewallPhoneStateListener for each Service. The Service name may change and the vendor may modify it.
Public ArrayList <String> getMultSimCardInfo () {// obtain the dual-card information, which is also tried by experience, I don't know what other vendors have. ArrayList <String> phoneServerList = new ArrayList <String> (); for (int I = 1; I <3; I ++) {try {String phoneServiceName; if (MiuiUtils. isMiuiV6 () {phoneServiceName = "phone. "+ String. valueOf (I-1);} else {phoneServiceName = "phone" + String. valueOf (I);} // try to get the service to see if IBinder iBinder = ServiceManager can be obtained. getService (phoneServiceName); if (iBinder = null) continue; ITelephony iTelephony = ITelephony. stub. asInterface (iBinder); if (iTelephony = null) continue; phoneServerList. add (phoneServiceName);} catch (Exception e) {e. printStackTrace () ;}/// this is the default phoneServerList. add (Context. TELEPHONY_SERVICE); return phoneServerList ;}
2. Hang up the phone
You can use the interface provided by the system service to stop the call. However, the call cannot be successful.
Public boolean endCall () {boolean callSuccess = false; ITelephony telephonyService = getTelephonyService (); try {if (telephonyService! = Null) {callSuccess = telephonyService. endCall () ;}} catch (RemoteException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} if (callSuccess = false) {Executor eS = Executors. newSingleThreadExecutor (); eS.exe cute (new Runnable () {@ Override public void run () {disconnectCall () ;}}); callSuccess = true;} return callSuccess ;} private boolean disconnectCall () {Runtime runtime = Runtime. getRuntime (); try {runtime.exe c ("service call phone 5 \ n");} catch (Exception exc) {exc. printStackTrace (); return false;} return true;} // The endCall cannot be used, And the killCall reflection call can be used to call the public static boolean killCall (Context context) again) {try {// Get the boring old TelephonyManager telephonyManager = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); // Get the getITelephony () method Class classTelephony = Class. forName (telephonyManager. getClass (). getName (); Method methodGetITelephony = classTelephony. getDeclaredMethod ("getITelephony"); // Ignore that the method is supposed to be private methodGetITelephony. setAccessible (true); // Invoke getITelephony () to get the ITelephony interface Object telephonyInterface = methodGetITelephony. invoke (telephonyManager); // Get the endCall method from ITelephony Class telephonyInterfaceClass = Class. forName (telephonyInterface. getClass (). getName (); Method methodEndCall = telephonyInterfaceClass. getDeclaredMethod ("endCall"); // Invoke endCall () methodEndCall. invoke (telephonyInterface);} catch (Exception ex) {// descrithings can go wrong with reflection CILS return false;} return true ;}
3. permission required for hanging up the phone
<uses-permission android:name="android.permission.CALL_PHONE" />
Extended reading:
This article focuses on the overall framework and mechanism of telephone listening.
Http://www.cnblogs.com/bastard/archive/2012/11/23/2784559.html
This article focuses on the meanings of variables in some api methods.
Http://blog.csdn.net/skiffloveblue/article/details/7491618