亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android來電監(jiān)聽和去電監(jiān)聽實(shí)現(xiàn)代碼

 更新時間:2017年06月13日 14:48:42   作者:攝氏三十七度  
本文是關(guān)于來點(diǎn)監(jiān)聽和去電監(jiān)聽展開問題,通過實(shí)例代碼講解,對android來電監(jiān)聽和去電監(jiān)聽的相關(guān)知識感興趣的朋友一起看看吧

我覺得寫文章就得寫得有用一些的,必須要有自己的思想,關(guān)于來電去電監(jiān)聽將按照下面三個問題展開

1、監(jiān)聽來電去電有什么用?

2、怎么監(jiān)聽,來電去電監(jiān)聽方式一樣嗎?

3、實(shí)戰(zhàn),有什么需要特別注意地方?

監(jiān)聽來電去電能干什么

1、能夠?qū)ΡO(jiān)聽到的電話做個標(biāo)識,告訴用戶這個電話是詐騙、推銷、廣告什么的

2、能夠針對那些特殊的電話進(jìn)行自動掛斷,避免打擾到用戶

來電去電的監(jiān)聽方式(不一樣的方式)

1、來電監(jiān)聽(PhoneStateListener)

  來電監(jiān)聽是使用PhoneStateListener類,使用方式是,將PhoneStateListener對象(一般是自己繼承PhoneStateListener類完成一些封裝)注冊到系統(tǒng)電話管理服務(wù)中去(TelephonyManager)

  然后通過PhoneStateListener的回調(diào)方法onCallStateChanged(int state, String incomingNumber) 實(shí)現(xiàn)來電的監(jiān)聽 (詳細(xì)實(shí)現(xiàn)可以參考后面給出的拓展閱讀部分)

  注冊監(jiān)聽

// phoneServiceName是服務(wù)名,一般是 "phone" --> Context.TELEPHONY_SERVICE
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(phoneServiceName);
if(telephonyManager != null) {
  try {
    // 注冊來電監(jiān)聽
    telephonyManager.listen(mTelephonyListener, PhoneStateListener.LISTEN_CALL_STATE);
  } catch(Exception e) {
    // 異常捕捉
  }
}

  PhoneStateListener的onCallStateChanged方法監(jiān)聽來電狀態(tài)

@Override
public void onCallStateChanged(int state, String incomingNumber) {
  switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
      // 電話掛斷
      break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
      // 來電響鈴
      break;
    case TelephonyManager.CALL_STATE_RINGING:
      // 來電接通
      break;
    default:
      break;
  }
}

  三種狀態(tài)源碼解釋

/** Device call state: No activity. */
public static final int CALL_STATE_IDLE = 0;  // 電話掛斷
/** Device call state: Ringing. A new call arrived and is
 * ringing or waiting. In the latter case, another call is
 * already active. */
public static final int CALL_STATE_RINGING = 1;  // 來電響鈴
/** Device call state: Off-hook. At least one call exists
 * that is dialing, active, or on hold, and no calls are ringing
 * or waiting. */
public static final int CALL_STATE_OFFHOOK = 2;  // 來電接通

2、去電監(jiān)聽(通過廣播來實(shí)現(xiàn))

// OutgoingCallListener繼承一個BroadcastReceiver
<receiver android:name="com.test.OutgoingCallListener" >
  <intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"/>
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
  </intent-filter>
</receiver>

實(shí)戰(zhàn),有什么需要特別注意地方

1、雙卡雙待的手機(jī)怎么獲取

  對于雙卡手機(jī),每張卡都對應(yīng)一個Service和一個PhoneStateListener,需要給每個服務(wù)注冊自己的PhoneStateListener,服務(wù)的名稱還會有點(diǎn)變化,廠商可能會修改

public ArrayList<String> getMultSimCardInfo() {
  // 獲取雙卡的信息,這個也是經(jīng)驗(yàn)嘗試出來的,不知道其他廠商有什么坑
  ArrayList<String> phoneServerList = new ArrayList<String>();
  for(int i = 1; i < 3; i++) {
    try {
      String phoneServiceName;
      if (MiuiUtils.isMiuiV6()) {
        phoneServiceName = "phone." + String.valueOf(i-1);
      } else {
        phoneServiceName = "phone" + String.valueOf(i);
      }
      // 嘗試獲取服務(wù)看是否能獲取到
      IBinder iBinder = ServiceManager.getService(phoneServiceName);
      if(iBinder == null) continue;
      ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
      if(iTelephony == null) continue;
      phoneServerList.add(phoneServiceName);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  // 這個是默認(rèn)的
  phoneServerList.add(Context.TELEPHONY_SERVICE);
  return phoneServerList;
}

2、掛斷電話

  掛斷電話使用系統(tǒng)服務(wù)提供的接口去掛斷,但是掛斷電話是個并不能保證成功的方法,所以會有多種方式掛斷同時使用,下面提供

public boolean endCall() {
  boolean callSuccess = false;
  ITelephony telephonyService = getTelephonyService();
  try {
    if (telephonyService != null) {
      callSuccess = telephonyService.endCall();
    }
  } catch (RemoteException e) {
    e.printStackTrace();
  } catch (Exception e){
    e.printStackTrace();
  }
  if (callSuccess == false) {
    Executor eS = Executors.newSingleThreadExecutor();
    eS.execute(new Runnable() {
      @Override
      public void run() {
        disconnectCall();
      }
    });
    callSuccess = true;
  }
  return callSuccess;
}
private boolean disconnectCall() {
  Runtime runtime = Runtime.getRuntime();
  try {
    runtime.exec("service call phone 5 \n");
  } catch (Exception exc) {
    exc.printStackTrace();
    return false;
  }
  return true;
}
// 使用endCall掛斷不了,再使用killCall反射調(diào)用再掛一次
public static boolean killCall(Context context) {
  try {
    // Get the boring old TelephonyManager
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    // Get the getITelephony() method
       Class classTelephony = Class.forName(telephonyManager.getClass().getName());
    Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
    // Ignore that the method is supposed to be private
      methodGetITelephony.setAccessible(true);
    // Invoke getITelephony() to get the ITelephony interface
      Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
    // Get the endCall method from ITelephony
      Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
    Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
    // Invoke endCall()
    methodEndCall.invoke(telephonyInterface);
  } catch (Exception ex) { // Many things can go wrong with reflection calls
    return false;
  }
  return true;
}

3、掛斷電話需要權(quán)限

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

以上所述是小編給大家介紹的Android來電監(jiān)聽和去電監(jiān)聽,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android UI更新的幾種方法總結(jié)

    Android UI更新的幾種方法總結(jié)

    這篇文章主要介紹了Android UI更新的幾種方法總結(jié)的相關(guān)資料,這里對Android UI 的幾種更新列出了,并附實(shí)例代碼,需要的朋友可以參考下
    2016-11-11
  • Android studio 解決logcat無過濾工具欄的操作

    Android studio 解決logcat無過濾工具欄的操作

    這篇文章主要介紹了Android studio 解決logcat無過濾工具欄的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • android okhttp的基礎(chǔ)使用【入門推薦】

    android okhttp的基礎(chǔ)使用【入門推薦】

    本文主要總結(jié)了Android著名網(wǎng)絡(luò)框架-okhttp的基礎(chǔ)使用。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Android編程實(shí)現(xiàn)計(jì)算兩個日期之間天數(shù)并打印所有日期的方法

    Android編程實(shí)現(xiàn)計(jì)算兩個日期之間天數(shù)并打印所有日期的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)計(jì)算兩個日期之間天數(shù)并打印所有日期的方法,涉及Android日期時間相關(guān)轉(zhuǎn)換與運(yùn)算操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android ListView異步加載圖片方法詳解

    Android ListView異步加載圖片方法詳解

    這篇文章主要介紹了Android ListView異步加載圖片方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ListView異步加載圖片的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-02-02
  • Android開發(fā)中用Kotlin編寫LiveData組件教程

    Android開發(fā)中用Kotlin編寫LiveData組件教程

    LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)
    2022-12-12
  • Android中button點(diǎn)擊后字體的變色效果

    Android中button點(diǎn)擊后字體的變色效果

    button的點(diǎn)擊效果無疑是非常簡單的,接下來通過本文給大家介紹下如何添加button點(diǎn)擊的字體顏色變化效果,感興趣的朋友一起看看吧
    2016-10-10
  • 深入了解Android?IO的底層原理

    深入了解Android?IO的底層原理

    這篇文章主要介紹了深入了解Android?IO的底層原理,IO有緩沖與非緩沖?IO、直接與非直接?IO、阻塞與非阻塞?IO、同步與異步?IO等分類,具體詳情感興趣的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • Android中獲得正在運(yùn)行的程序和系統(tǒng)服務(wù)的方法

    Android中獲得正在運(yùn)行的程序和系統(tǒng)服務(wù)的方法

    這篇文章主要介紹了Android中獲得正在運(yùn)行的程序和系統(tǒng)服務(wù)的方法,分別是對ActivityManager.RunningAppProcessInfo類和ActivityManager.RunningServiceInfo類的使用,需要的朋友可以參考下
    2016-02-02
  • 性能分析工具Systrace的使用及說明

    性能分析工具Systrace的使用及說明

    這篇文章主要介紹了性能分析工具Systrace的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論