How to operate Android listeners correctly

Source: Internet
Author: User

Anyone who knows about smartphones knows that one of the most widely used mobile phone operating systems is the Android open-source mobile phone operating system. How should we implement the call listening function in this system? Here we will introduce in detail how to implement Android listening for calls.

  • Android logcat Application Guide
  • How to Use Animation for Android
  • Detailed description of how Android starts a Java program APPLICATION
  • Analysis on practical application of Android Transplantation
  • Explanation of Android vibration code

When developing an application, we hope to be able to listen for incoming calls from the phone so as to pause the music player and perform other operations. After the call is over, play back again. On the Android platform, you can use TelephonyManager and PhoneStateListener to complete this task.

As a Service interface, TelephonyManager provides users with telephone-related content query, such as IMEI and LineNumber1. Use the following code to obtain the TelephonyManager instance.

 
 
  1. TelephonyManager mTelephonyMgr = (TelephonyManager) this  
  2. .getSystemService(Context.TELEPHONY_SERVICE); 

On the Android platform, PhoneStateListener is a useful listener for listening to the phone status, such as call status and Connection Service. Android listens to a call as follows:

 
 
  1. public void onCallForwardingIndicatorChanged(boolean cfi)  
  2. public void onCallStateChanged(int state, 
    String incomingNumber)  
  3. public void onCellLocationChanged(CellLocation location)  
  4. public void onDataActivity(int direction)  
  5. public void onDataConnectionStateChanged(int state)  
  6. public void onMessageWaitingIndicatorChanged(boolean mwi)  
  7. public void onServiceStateChanged
    (ServiceState serviceState)  
  8. public void onSignalStrengthChanged(int asu) 

Here we only need to override the onCallStateChanged () method to listen to the call status. In TelephonyManager, three statuses are defined, namely, RINGING, offline hook, and IDLE. We can know the current call status through the state value.

After obtaining the TelephonyManager interface, you can call the listen () method to implement Android listening for calls.

 
 
  1. mTelephonyMgr.listen(new TeleListener(),  
  2. PhoneStateListener.LISTEN_CALL_STATE); 

The following is a simple test example. It only adds the call status to TextView.

 
 
  1. package com.j2medev;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.os.Bundle;  
  5. import android.telephony.PhoneStateListener;  
  6. import android.telephony.TelephonyManager;  
  7. import android.util.Log;  
  8. import android.widget.TextView;  
  9. public class Telephony extends Activity {  
  10. private static final String TAG = "Telephony";  
  11. TextView view = null;  
  12. @Override  
  13. protected void onCreate(Bundle savedInstanceState) {  
  14. super.onCreate(savedInstanceState);  
  15. TelephonyManager mTelephonyMgr = (TelephonyManager) this  
  16. .getSystemService(Context.TELEPHONY_SERVICE);  
  17. mTelephonyMgr.listen(new TeleListener(),  
  18. PhoneStateListener.LISTEN_CALL_STATE);  
  19. view = new TextView(this);  
  20. view.setText("listen the state of phone\n");  
  21. setContentView(view);  
  22. }  
  23. class TeleListener extends PhoneStateListener {  
  24. @Override  
  25. public void onCallStateChanged(int state, 
    String incomingNumber) {  
  26. super.onCallStateChanged(state, incomingNumber);  
  27. switch (state) {  
  28. case TelephonyManager.CALL_STATE_IDLE: {  
  29. Log.e(TAG, "CALL_STATE_IDLE");  
  30. view.append("CALL_STATE_IDLE " + "\n");  
  31. break;  
  32. }  
  33. case TelephonyManager.CALL_STATE_OFFHOOK: {  
  34. Log.e(TAG, "CALL_STATE_OFFHOOK");  
  35. view.append("CALL_STATE_OFFHOOK" + "\n");  
  36. break;  
  37. }  
  38. case TelephonyManager.CALL_STATE_RINGING: {  
  39. Log.e(TAG, "CALL_STATE_RINGING");  
  40. view.append("CALL_STATE_RINGING" + "\n");  
  41. break;  
  42. }  
  43. default:  
  44. break;  
  45. }  
  46. }  
  47. }  

Do not forget to add permission in AndroidManifest. xml.

 
 
  1. < uses-permission android:name=
    "android.permission.READ_PHONE_STATE" /> 

The specific operation method for Android to listen to a call is described here.

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.