最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android來(lái)電監(jiān)聽(tīng)和去電監(jiān)聽(tīng)實(shí)現(xiàn)代碼

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

我覺(jué)得寫(xiě)文章就得寫(xiě)得有用一些的,必須要有自己的思想,關(guān)于來(lái)電去電監(jiān)聽(tīng)將按照下面三個(gè)問(wèn)題展開(kāi)

1、監(jiān)聽(tīng)來(lái)電去電有什么用?

2、怎么監(jiān)聽(tīng),來(lái)電去電監(jiān)聽(tīng)方式一樣嗎?

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

監(jiān)聽(tīng)來(lái)電去電能干什么

1、能夠?qū)ΡO(jiān)聽(tīng)到的電話(huà)做個(gè)標(biāo)識(shí),告訴用戶(hù)這個(gè)電話(huà)是詐騙、推銷(xiāo)、廣告什么的

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

來(lái)電去電的監(jiān)聽(tīng)方式(不一樣的方式)

1、來(lái)電監(jiān)聽(tīng)(PhoneStateListener)

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

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

  注冊(cè)監(jiān)聽(tīng)

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

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

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

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

/** Device call state: No activity. */
public static final int CALL_STATE_IDLE = 0;  // 電話(huà)掛斷
/** 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;  // 來(lái)電響鈴
/** 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;  // 來(lái)電接通

2、去電監(jiān)聽(tīng)(通過(guò)廣播來(lái)實(shí)現(xiàn))

// OutgoingCallListener繼承一個(gè)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ī)怎么獲取

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

public ArrayList<String> getMultSimCardInfo() {
  // 獲取雙卡的信息,這個(gè)也是經(jīng)驗(yàn)嘗試出來(lái)的,不知道其他廠(chǎng)商有什么坑
  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();
    }
  }
  // 這個(gè)是默認(rèn)的
  phoneServerList.add(Context.TELEPHONY_SERVICE);
  return phoneServerList;
}

2、掛斷電話(huà)

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

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、掛斷電話(huà)需要權(quán)限

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

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

相關(guān)文章

最新評(píng)論

婺源县| 象州县| 东山县| 洛宁县| 麻栗坡县| 千阳县| 东乡族自治县| 汾西县| 宜州市| 巴南区| 定日县| 德令哈市| 新晃| 泾阳县| 扎兰屯市| 辰溪县| 蒙山县| 龙泉市| 鄂托克前旗| 出国| 阳东县| 潞城市| 澄城县| 基隆市| 赣榆县| 武鸣县| 佛山市| 哈尔滨市| 威信县| 桦川县| 仁怀市| 南岸区| 新干县| 札达县| 浪卡子县| 日喀则市| 崇阳县| 西乡县| 新沂市| 鄂尔多斯市| 朝阳县|