android電話狀態監聽

實現手機電話狀態的監聽,主要依靠兩個類:TelephoneManger和PhoneStateListener。
TelephonseManger提供了取得手機基本服務的信息的一種方式。因此應用程序可以使用TelephonyManager來探測手機基本服務的情況。應用程序可以註冊listener來監聽電話狀態的改變。我們不能對TelephonyManager進行實例化,只能通過獲取服務的形式:
Context.getSystemService(Context.TELEPHONY_SERVICE);
注意:對手機的某些信息進行讀取是需要一定許可(permission)的。
 
主要靜態成員常量:(它們對應PhoneStateListener.LISTEN_CALL_STATE所監聽到的內容)
int CALL_STATE_IDLE   空閒狀態,沒有任何活動。
int CALL_STATE_OFFHOOK  摘機狀態,至少有個電話活動。該活動或是撥打(dialing)或是通話,或是 on hold。並且沒有電話是ringing or waiting
int CALL_STATE_RINGING  來電狀態,電話鈴聲響起的那段時間或正在通話又來新電,新來電話不得不等待的那段時間。
 
手機通話狀態在廣播中的對應值
 
EXTRA_STATE_IDLE 它在手機通話狀態改變的廣播中,用於表示CALL_STATE_IDLE狀態
EXTRA_STATE_OFFHOOK 它在手機通話狀態改變的廣播中,用於表示CALL_STATE_OFFHOOK狀態
EXTRA_STATE_RINGING 它在手機通話狀態改變的廣播中,用於表示CALL_STATE_RINGING狀態
ACTION_PHONE_STATE_CHANGED 在廣播中用ACTION_PHONE_STATE_CHANGED這個Action來標示通話狀態改變的廣播(intent)。
注:需要許可READ_PHONE_STATE。
String EXTRA_INCOMING_NUMBER 
在手機通話狀態改變的廣播,用於從extra取來電號碼。
String EXTRA_STATE  在通話狀態改變的廣播,用於從extra取來通話狀態。
 
主要成員函數
public int getCallState() 取得手機的通話狀態。
public CellLocation getCellLocation () 返回手機當前所處的位置。如果當前定位服務不可用,則返回null
注:需要許可(Permission)ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity () 返回當前數據連接活動狀態的情況。
public int getDataState () 返回當前數據連接狀態的情況。
public String getDeviceId ()
返回手機的設備ID。比如對於GSM的手機來說是IMEI碼,對於CDMA的手機來說MEID碼或ESN碼。如果讀取失敗,則返回null。
 
如何實現電話狀態的監聽呢?
 
Android在電話狀態改變是會發送action爲android.intent.action.PHONE_STATE的廣播,而撥打電話時會發送action爲android.intent.action.NEW_OUTGOING_CALL的廣播,但是我看了下開發文檔,暫時沒發現有來電時的廣播。通過自定義廣播接收器,接受上述兩個廣播便可。
 
Java代碼:
package com.pocketdigi.phonelistener;
 
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
 
public class PhoneReceiver extends BroadcastReceiver {
 
 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("action"+intent.getAction());
  //如果是去電
  if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
     String phoneNumber = intent
     .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
     Log.d(TAG, "call OUT:" + phoneNumber); 
   }else{
   //查了下android文檔,貌似沒有專門用於接收來電的action,所以,非去電即來電.
   //如果我們想要監聽電話的撥打狀況,需要這麼幾步 :
    * 第一:獲取電話服務管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
    * 第二:通過TelephonyManager註冊我們要監聽的電話狀態改變事件。manager.listen(new MyPhoneStateListener(),
    * PhoneStateListener.LISTEN_CALL_STATE);這裏的PhoneStateListener.LISTEN_CALL_STATE就是我們想要
    * 監聽的狀態改變事件,初次之外,還有很多其他事件哦。
    * 第三步:通過extends PhoneStateListener來定製自己的規則。將其對象傳遞給第二步作爲參數。
    * 第四步:這一步很重要,那就是給應用添加權限。android.permission.READ_PHONE_STATE


   TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); 
   tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
   //設置一個監聽器
  }
 }
 PhoneStateListener listener=new PhoneStateListener(){
 
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
   //注意,方法必須寫在super方法後面,否則incomingNumber無法獲取到值。
   super.onCallStateChanged(state, incomingNumber);
   switch(state){
   case TelephonyManager.CALL_STATE_IDLE:
    System.out.println("掛斷");
    break;
   case TelephonyManager.CALL_STATE_OFFHOOK:
    System.out.println("接聽");
    break;
   case TelephonyManager.CALL_STATE_RINGING:
    System.out.println("響鈴:來電號碼"+incomingNumber);
    //輸出來電號碼
    break;
   }
  }
 };
}
要在AndroidManifest.xml註冊廣播接收器:
<receiver android:name=".PhoneReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.PHONE_STATE"/>  
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  
        </intent-filter>  
       </receiver>  
 <receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver>

還要添加權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  

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



轉載自:http://www.cnblogs.com/haowenbiao/archive/2012/08/15/2639579.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章