Resolution of getting phone status in Android games to pause or continue the game

Source: Internet
Author: User
Tags android games

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.

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.
Java code:
Copy codeThe Code is as follows: TelephonyManager mTelephonyMgr = (TelephonyManager) this. 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:
Java code:
Copy codeThe Code is as follows: public void onCallForwardingIndicatorChanged (boolean cfi)
Public void onCallStateChanged (int state, String incomingNumber)
Public void onCellLocationChanged (CellLocation location)
Public void onDataActivity (int direction)
Public void onDataConnectionStateChanged (int state)
Public void onMessageWaitingIndicatorChanged (boolean mwi)
Public void onServiceStateChanged (ServiceState serviceState)
Public void onSignalStrengthChanged (int asu)

Here we only need to override the onCallStateChanged () method to listen to the call status. TelephonyManager defines three statuses: RINGING, OFFHOOK, and IDLE. Now we 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.
Java code:
MTelephonyMgr. listen (new TeleListener (), PhoneStateListener. LISTEN_CALL_STATE );
The following is a simple test example. It only adds the call status to TextView.
Java code:
Copy codeThe Code is as follows: package eoe. demo;
Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. telephony. PhoneStateListener;
Import android. telephony. TelephonyManager;
Import android. util. Log;
Import android. widget. TextView;
Public class Telephony extends Activity {
Private static final String TAG = "Telephony ";
TextView view = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TelephonyManager mTelephonyMgr = (TelephonyManager) this. getSystemService (Context. TELEPHONY_SERVICE );
MTelephonyMgr. listen (new TeleListener (), PhoneStateListener. LISTEN_CALL_STATE );
View = new TextView (this );
View. setText ("listen the state of phone \ n ");
SetContentView (view );
}
Class TeleListener extends PhoneStateListener {
@ Override
Public void onCallStateChanged (int state, String incomingNumber ){
Super. onCallStateChanged (state, incomingNumber );
Switch (state ){
Case TelephonyManager. CALL_STATE_IDLE :{
Log. e (TAG, "CALL_STATE_IDLE ");
View. append ("CALL_STATE_IDLE" + "\ n ");
Break;
}
Case TelephonyManager. CALL_STATE_OFFHOOK :{
Log. e (TAG, "CALL_STATE_OFFHOOK ");
View. append ("CALL_STATE_OFFHOOK" + "\ n ");
Break;
}
Case TelephonyManager. CALL_STATE_RINGING :{
Log. e (TAG, "CALL_STATE_RINGING ");
View. append ("CALL_STATE_RINGING" + "\ n ");
Break;
}
Default: break;
}
}
}
}

Do not forget to add permission in AndroidManifest. xml.
Java code:
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>

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.