欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解析Android游戲中獲取電話狀態(tài)進(jìn)行游戲暫?;蚶^續(xù)的解決方法

 更新時(shí)間:2013年05月20日 17:36:39   作者:  
本篇文章是對在Android游戲中獲取電話狀態(tài)進(jìn)行游戲暫?;蚶^續(xù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
對智能手機(jī)有所了解的朋友都知道其中一個(gè)應(yīng)用廣泛的手機(jī)操作系統(tǒng)Android 開源手機(jī)操作系統(tǒng)。那么在這一系統(tǒng)中想要實(shí)現(xiàn)通話的監(jiān)聽功能的話,我們應(yīng)當(dāng)如何操作呢?在這里就為大家詳細(xì)介紹了Android監(jiān)聽通話的相關(guān)實(shí)現(xiàn)方法。

開發(fā)應(yīng)用程序的時(shí)候,我們希望能夠監(jiān)聽電話的呼入,以便執(zhí)行暫停音樂播放器等操作,當(dāng)電話結(jié)束之后,再次恢復(fù)播放。在Android平臺(tái)可以通過TelephonyManager和PhoneStateListener來完成此任務(wù)。
TelephonyManager作為一個(gè)Service接口提供給用戶查詢電話相關(guān)的內(nèi)容,比如IMEI,LineNumber1等。通過下面的代碼即可獲得TelephonyManager的實(shí)例。
java代碼:
復(fù)制代碼 代碼如下:

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

在Android平臺(tái)中,PhoneStateListener是個(gè)很有用的監(jiān)聽器,用來監(jiān)聽電話的狀態(tài),比如呼叫狀態(tài)和連接服務(wù)等。Android監(jiān)聽通話方法如下所示:
java代碼:
復(fù)制代碼 代碼如下:

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)

這里我們只需要覆蓋onCallStateChanged()方法即可監(jiān)聽呼叫狀態(tài)。在TelephonyManager中定義了三種狀態(tài),分別是振鈴(RINGING),摘機(jī)(OFFHOOK)和空閑(IDLE),我們通過state的值就知道現(xiàn)在的電話狀態(tài)了。
獲得了TelephonyManager接口之后,調(diào)用listen()方法即可實(shí)現(xiàn)Android監(jiān)聽通話。
java代碼:
mTelephonyMgr.listen(new TeleListener(),  PhoneStateListener.LISTEN_CALL_STATE);
下面是個(gè)簡單的測試?yán)?,只是把呼叫狀態(tài)追加到TextView之上。
java代碼:
復(fù)制代碼 代碼如下:

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; 

}
}
}

不要忘記在AndroidManifest.xml里面添加個(gè)permission.
java代碼:
< uses-permission android:name="android.permission.READ_PHONE_STATE" />

相關(guān)文章

最新評(píng)論