Android Phone status monitor (call and go) implementation code _android

Source: Internet
Author: User
The realization of mobile phone status monitoring, mainly rely on two categories: Telephonemanger and Phonestatelistener.
Telephonsemanger provides a way to obtain information about the basic services of a mobile phone. So the application can use Telephonymanager to detect the basic service of the mobile phone. Applications can register listener to listen for changes to the phone's status. We cannot instantiate the Telephonymanager, only by getting the service in the form of:
Context.getsystemservice (Context.telephony_service);
Note: Some of the information on the mobile phone to read is required a certain license (permission).
Primary static member constants: (they correspond to what the Phonestatelistener.listen_call_state hears)
int Call_state_idle idle without any activity.
int Call_state_offhook pick up the state, at least have a phone activity. The activity either dials (dialing) or calls, or on hold. And no phone is ringing or waiting
int call_state_ringing caller status, when the phone rang, or when the call came to new electricity, the new phone had to wait for the period of time.
The corresponding value of mobile phone status in broadcast
Extra_state_idle it in the broadcast of mobile phone status change, used to indicate call_state_idle status
Extra_state_offhook it in the broadcast of mobile phone status change, used to indicate call_state_offhook status
Extra_state_ringing it in the broadcast of mobile phone status change, used to indicate call_state_ringing status
Action_phone_state_changed uses the action_phone_state_changed action on the radio to mark the broadcast (intent) of the change in the status of the call.
Note: License read_phone_state is required.
String Extra_incoming_number
In the mobile phone status change broadcast, used to pick up the caller number from the extra.
String Extra_state A broadcast that changes in the status of a call, used to call from EXTRA.

Primary member function
public int getcallstate () Gets the phone call status.
Public celllocation getcelllocation () returns the location where the phone is currently located. Returns null if the current location service is not available
Note: Require permission (Permission) access_coarse_location or access_fine_location.
public int getdataactivity () returns the status of the current data connection activity.
public int getdatastate () returns the condition of the current data connection state.
Public String Getdeviceid ()
Returns the device ID of the cell phone. For example, for GSM mobile phone is IMEI code, for CDMA mobile phone Meid code or ESN code. If the read fails, NULL is returned.

How to monitor the status of the phone?
Android will send an action-Android.intent.action.PHONE_STATE broadcast when the phone status changes, and the action will be sent when the call is Android.intent.action.NEW_ Outgoing_call broadcast, but I looked at the development document, and temporarily did not find a call to broadcast. By customizing the broadcast receiver, you can accept both broadcasts.
Java code:
Copy Code code as follows:

Package Com.pocketdigi.phonelistener;
Import Android.app.Service;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
public class Phonereceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
System.out.println ("Action" +intent.getaction ());
If you're going to power
if (Intent.getaction (). Equals (Intent.action_new_outgoing_call)) {
String PhoneNumber = Intent
. Getstringextra (Intent.extra_phone_number);
LOG.D (TAG, "call out:" + PhoneNumber);
}else{
Check the Android document, seemingly not specifically used to receive calls to the action, so, not to power is called.
If we want to monitor the dialing status of the phone, we need a few steps:
* First: Get phone service Manager Telephonymanager manager = This.getsystemservice (telephony_service);
* Second: Register the phone status change event that we are listening to by Telephonymanager. Manager.listen (New Myphonestatelistener (),
* phonestatelistener.listen_call_state); The phonestatelistener.listen_call_state here is what we want
* Monitoring the state of change events, the first time, there are many other events oh.
* Step three: Customize your rules by extends Phonestatelistener. Passes its object to the second step as an argument.
* Fourth Step: This step is important, that is to add permissions to the application. Android.permission.READ_PHONE_STATE
Telephonymanager TM = (Telephonymanager) context.getsystemservice (Service.telephony_service);
Tm.listen (Listener, phonestatelistener.listen_call_state);
Set up a listener
}
}
Phonestatelistener listener=new Phonestatelistener () {
@Override
public void oncallstatechanged (int state, String incomingnumber) {
Note that the method must be written behind the super method, otherwise incomingnumber cannot get the value.
Super.oncallstatechanged (state, Incomingnumber);
Switch (state) {
Case Telephonymanager.call_state_idle:
System.out.println ("Hang Up");
Break
Case Telephonymanager.call_state_offhook:
SYSTEM.OUT.PRINTLN ("Answer listen");
Break
Case telephonymanager.call_state_ringing:
System.out.println ("Bell: Caller ID" +incomingnumber);
Output caller number
Break
}
}
};
}

To register a broadcast receiver in Androidmanifest.xml:
Copy Code code as follows:

<receiver android:name= ". Phonereceiver ">
<intent-filter>
<action android:name= "Android.intent.action.PHONE_STATE"/>
<action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
<receiver android:name= ". Phonereceiver "> <intent-filter> <action android:name=" Android.intent.action.PHONE_STATE "/> < Action android:name= "Android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>

Also add permissions:
Copy Code code as follows:

<uses-permission android:name= "Android.permission.READ_PHONE_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS" ></uses-permission>
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.