Resolve the solution of getting phone status to suspend or continue in Android game _android

Source: Internet
Author: User
Friends familiar with smartphones know one of the widely used mobile operating system Android open source mobile OS. So what should we do if we want to realize the listening function of the call in this system? Here is a detailed introduction to the Android listening to the relevant implementation of the call.

When developing an application, we want to be able to listen for incoming calls to perform operations such as pausing a music player, and then resuming playback when the phone is finished. This can be done on the Android platform through Telephonymanager and Phonestatelistener.
Telephonymanager serves as a service interface for users to inquire about phone-related content, such as Imei,linenumber1. An instance of Telephonymanager can be obtained by using the following code.
Java code:
Copy Code code as follows:

Telephonymanager mtelephonymgr = (Telephonymanager) this. Getsystemservice (Context.telephony_service);

In the Android platform, Phonestatelistener is a very useful listener for monitoring the status of the phone, such as call status and connection services. The Android listening call method looks like this:
Java code:
Copy Code code 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 just need to overwrite the oncallstatechanged () method to listen for the call status. In Telephonymanager, three states are defined, namely ringing (ringing), picking (Offhook) and Idle (IDLE), and we know the current phone status by the value of state.
After the Telephonymanager interface is obtained, the Listen () method is invoked to implement the Android listening call.
Java code:
Mtelephonymgr.listen (New Telelistener (), phonestatelistener.listen_call_state);
Here is a simple test example that appends the call status to the TextView.
Java code:
Copy Code code 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;
}
}
}
}

don't forget to add a permission to the 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.