Android listener (2) precisely monitors the call status (call, dialing, and hanging up) in Broadcast mode

Source: Internet
Author: User

The previous article has introduced the basic android system event monitoring. If you do not know about this, go to the following step: Android listener implementation (1) Listen to system events in Broadcast mode.

Currently, an application I wrote needs to process the call hangs. After querying APIs and online data, I found that there are only three types of listener for the call status:

Public static final int CALL_STATE_IDLE = 0 // No call status public static final int CALL_STATE_RINGING = 1 // public static final int CALL_STATE_OFFHOOK = 2 // call in progress

After checking the information, I found that the official website does not provide an api for the end of the call. However, the notification of a change in the call status cannot meet my handling requirements for hanging up, first, let's analyze how to obtain the current call status in a more precise manner, including incoming calls, calls, and disconnections ).

1. We want to get the current call status

2. Get the previous call status

3. Compare the two statuses

For these analysis processes, we need to save the previous call status, which can be implemented in various ways (file stream, sqiite, etc.). I implemented it using sqiite, the main reason is that the project I wrote needs to use sqlite, So I simply used it directly. If you are not familiar with these shoes, you can use file streams or other methods. Some children's shoes may ask, "why don't I use a static variable in the Broadcast class to keep the original call status ?". This is because the onrecieve () method of the broadcast class will be called every time the system call status changes. After the method is executed, it will be recycled by the system. Each time it is a re-created class, so every time this variable is an initial value.

Okay, I don't need to talk about it anymore. Let's go directly to the Code. This time I am still using the source code in the previous article, so I only write the key part in this example, for other information, see the source code in the download link below.


Package com. ls. broadcast; import com. ls. broadcast. util. sqliteUtil; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. phoneStateListener; import android. telephony. telephonyManager;/*** System Event listener ** @ author ls * @ version 2013-07-29 **/public class MyPhoneBroadcastListener extends BroadcastReceiver {/*** no phone call, in the general status value */public static final int CALL_TYPE_IDEL = 0;/*** mobile phone call status value */public static final int CALL_TYPE_CALLING = 1; /*** phone ringing status value */public static final int CALL_TYPE_RING = 2;/*** current phone call status value */private int currentState = CALL_TYPE_IDEL; /*** original phone call status value */private int oldState = CALL_TYPE_IDEL;/*** database Helper class, which only helps us to access the status value, you can use a file stream to implement */private SqliteUtil sqlUtil = null; private MyPhoneListener listener; @ Override // when a listener event occurs, the system will call this method public void onReceive (Context context, intent intent) {// For detailed monitoring, we need to operate TelephonyManager and set the listener for it. Then he will give us feedback // get the system TelephonyManager tpManager = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); listener = new MyPhoneListener (); // create the tpManager listener. listen (listener, PhoneStateListener. LISTEN_CALL_STATE); // sets the listener sqlUtil = new SqliteUtil (context);} private class MyPhoneListener extends PhoneStateListener {@ Override // when the phone status changes, the system will call this method public void onCallStateChanged (int state, String incomingNumber) {// first obtain the current state value oldState = sqlUtil. getOldState (); switch (state) {case TelephonyManager. CALL_STATE_IDLE: currentState = CALL_TYPE_IDEL; break; case TelephonyManager. CALL_STATE_OFFHOOK: currentState = CALL_TYPE_CALLING; break; case TelephonyManager. CALL_STATE_RINGING: currentState = CALL_TYPE_RING; break;} // when the call status changes if (oldState = CALL_TYPE_RING & currentState = CALL_TYPE_CALLING) {System. out. println ("Answer");} else if (oldState = CALL_TYPE_CALLING & currentState = CALL_TYPE_IDEL) {System. out. println ("hang up");} if (oldState = CALL_TYPE_IDEL & currentState = CALL_TYPE_CALLING) {System. out. println ("dial");} sqlUtil. updateState (currentState );}}}


In this example, sqlutil is used to store and read the status of the last call. To compare and analyze the status with the current status, if the kids shoes are not familiar with sqiite processing, please switch to the implementation method you are familiar.


2. SqliteUtil class


Package com. ls. broadcast. util; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; import com. ls. broadcast. myPhoneBroadcastListener;/*** Database Help class * @ author ls * @ version 2013-08-02 */public class SqliteUtil extends SQLiteOpenHelper {private static final String DB_NAME = "broadcast. db "; privat E static final String TABLE_NAME = "config"; private SQLiteDatabase db = null; public SqliteUtil (Context context) {super (context, DB_NAME, null, 1 ); // TODO Auto-generated constructor stub} public int getOldState () {int state = 0; db = getReadableDatabase (); Cursor cursor = db. rawQuery ("select * from" + TABLE_NAME, null); if (cursor. moveToFirst () {state = cursor. getInt (cursor. getColumnInde X ("flag");} db. close (); return state;} public void updateState (int state) {db = getWritableDatabase (); db. beginTransaction (); db.exe cSQL ("update" + TABLE_NAME + "set flag =" + state); db. setTransactionSuccessful (); db. endTransaction (); db. close ();}/*** call the first time you create a database (that is, the database DB_NAME does not exist on the machine) * <br> the system will help you create the database file DB_NAME, you only need to create a table. * // @ Override public void onCreate (SQLiteDatabase db) {// TODO Auto-generated method stub this. db = db; db.exe cSQL ("create table" + TABLE_NAME + "(flag Integer)"); db.exe cSQL ("insert into" + TABLE_NAME + "values (" + MyPhoneBroadcastListener. CALL_TYPE_IDEL + ")") ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }}

Android call status listening source code

This article is from the "Domain" blog, please be sure to keep this source http://listar.blog.51cto.com/6242289/1264091

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.