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

android實(shí)現(xiàn)接通和掛斷電話

 更新時(shí)間:2018年05月05日 13:47:56   作者:WillenWu  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)接通和掛斷電話功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android實(shí)現(xiàn)接通和掛斷電話的具體代碼,供大家參考,具體內(nèi)容如下

關(guān)鍵代碼:【PhoneUtils類】

package com.ebupt.phonerecorddemo.server; 
 
import java.lang.reflect.Method; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.KeyEvent; 
 
import com.android.internal.telephony.ITelephony; 
 
public class PhoneUtils { 
 static String TAG = "PhoneUtils"; 
 /** 
  * 從TelephonyManager中實(shí)例化ITelephony,并返回 
  */ 
 static public ITelephony getITelephony(TelephonyManager telMgr) 
   throws Exception { 
  Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod( 
    "getITelephony"); 
  getITelephonyMethod.setAccessible(true);// 私有化函數(shù)也能使用 
  return (ITelephony) getITelephonyMethod.invoke(telMgr); 
 } 
  
 //自動(dòng)接聽 
 public static void autoAnswerPhone(Context c,TelephonyManager tm) { 
  try { 
   Log.i(TAG, "autoAnswerPhone"); 
   ITelephony itelephony = getITelephony(tm); 
   // itelephony.silenceRinger(); 
   itelephony.answerRingingCall(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
   try { 
    Log.e(TAG, "用于Android2.3及2.3以上的版本上"); 
    Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent); 
    c.sendOrderedBroadcast(intent, 
      "android.permission.CALL_PRIVILEGED"); 
    intent = new Intent("android.intent.action.MEDIA_BUTTON"); 
    keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent); 
    c.sendOrderedBroadcast(intent, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    localIntent1.putExtra("state", 1); 
    localIntent1.putExtra("microphone", 1); 
    localIntent1.putExtra("name", "Headset"); 
    c.sendOrderedBroadcast(localIntent1, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    localIntent2.putExtra("android.intent.extra.KEY_EVENT", 
      localKeyEvent1); 
    c.sendOrderedBroadcast(localIntent2, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    localIntent3.putExtra("android.intent.extra.KEY_EVENT", 
      localKeyEvent2); 
    c.sendOrderedBroadcast(localIntent3, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    localIntent4.putExtra("state", 0); 
    localIntent4.putExtra("microphone", 1); 
    localIntent4.putExtra("name", "Headset"); 
    c.sendOrderedBroadcast(localIntent4, 
      "android.permission.CALL_PRIVILEGED"); 
   } catch (Exception e2) { 
    e2.printStackTrace(); 
    Intent meidaButtonIntent = new Intent( 
      Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); 
    c.sendOrderedBroadcast(meidaButtonIntent, null); 
   } 
  } 
 } 
  
 //自動(dòng)掛斷 
 public static void endPhone(Context c,TelephonyManager tm) { 
  try { 
   Log.i(TAG, "endPhone"); 
   ITelephony iTelephony; 
   Method getITelephonyMethod = TelephonyManager.class 
     .getDeclaredMethod("getITelephony", (Class[]) null); 
   getITelephonyMethod.setAccessible(true); 
   iTelephony = (ITelephony) getITelephonyMethod.invoke(tm, 
     (Object[]) null); 
   // 掛斷電話 
   iTelephony.endCall(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
 
} 

需要用到的ITelephony.aidl:

package com.android.internal.telephony; 
 /** 
 * Interface used to interact with the phone. Mostly this is used by the 
 * TelephonyManager class. A few places are still using this directly. 
 * Please clean them up if possible and use TelephonyManager instead. 
 * {@hide} 
 */ 
 
interface ITelephony {  
 /**  
 * End call or go to the Home screen 
 * @return whether it hung up  
 */  
 boolean endCall();  
  
 /**  
 * Answer the currently-ringing call.  
 *  
 * If there's already a current active call, that call will be  
 * automatically put on hold. If both lines are currently in use, the  
 * current active call will be ended.  
 *  
 * TODO: provide a flag to let the caller specify what policy to use  
 * if both lines are in use. (The current behavior is hardwired to  
 * "answer incoming, end ongoing", which is how the CALL button  
 * is specced to behave.)  
 *  
 * TODO: this should be a oneway call (especially since it's called  
 * directly from the key queue thread).  
 */  
 void answerRingingCall(); 
  
 /** 
  * Silence the ringer if an incoming call is currently ringing. 
  * (If vibrating, stop the vibrator also.) 
  * 
  * It's safe to call this if the ringer has already been silenced, or 
  * even if there's no incoming call. (If so, this method will do nothing.) 
  * 
  * TODO: this should be a oneway call too (see above). 
  *  (Actually *all* the methods here that return void can 
  *  probably be oneway.) 
  */ 
 void silenceRinger(); 
  
 /** 
  * Allow mobile data connections. 
  */ 
 boolean enableDataConnectivity(); 
 
 /** 
  * Disallow mobile data connections. 
  */ 
 boolean disableDataConnectivity(); 
 
 /** 
  * Report whether data connectivity is possible. 
  */ 
 boolean isDataConnectivityPossible(); 
} 

監(jiān)聽通話廣播【PhoneReceiver.java】:

package com.ebupt.phonerecorddemo.server; 
 
import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
 
public class PhoneReceiver extends BroadcastReceiver { 
 String TAG = "PhoneReceiver"; 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  TelephonyManager tm = (TelephonyManager) context 
    .getSystemService(Service.TELEPHONY_SERVICE); 
  switch (tm.getCallState()) { 
  case TelephonyManager.CALL_STATE_OFFHOOK:// 電話打進(jìn)來(lái)接通狀態(tài);電話打出時(shí)首先監(jiān)聽到的狀態(tài)。 
   Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK"); 
   break; 
  case TelephonyManager.CALL_STATE_RINGING:// 電話打進(jìn)來(lái)狀態(tài) 
   Log.i("onCallStateChanged", "CALL_STATE_RINGING"); 
   PhoneUtils.autoAnswerPhone(context,tm); 
   break; 
  case TelephonyManager.CALL_STATE_IDLE:// 不管是電話打出去還是電話打進(jìn)來(lái)都會(huì)監(jiān)聽到的狀態(tài)。 
   Log.i("onCallStateChanged", "CALL_STATE_IDLE"); 
   break; 
  } 
 } 
 
  
} 

在上面類適當(dāng)?shù)牡胤郊由蠏鞌嗷蚪油娫挼拇a即可。

最后別忘記在AndroidManifest.xml里聲明和注冊(cè)權(quán)限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" > 
</uses-permission> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" > 
</uses-permission> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<receiver android:name="com.ebupt.phonerecorddemo.server.PhoneReceiver" > 
 <intent-filter android:priority="2147483647" > 
 <action android:name="android.intent.action.PHONE_STATE" > 
 </action> 
 </intent-filter> 
</receiver> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio應(yīng)用開發(fā)集成百度語(yǔ)音合成使用方法實(shí)例講解

    Android Studio應(yīng)用開發(fā)集成百度語(yǔ)音合成使用方法實(shí)例講解

    這篇文章主要介紹了Android Studio應(yīng)用開發(fā)集成百度語(yǔ)音合成使用方法實(shí)例講解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • Android提高Service優(yōu)先級(jí)的方法分析

    Android提高Service優(yōu)先級(jí)的方法分析

    這篇文章主要介紹了Android提高Service優(yōu)先級(jí)的方法,簡(jiǎn)單講述了Service優(yōu)先級(jí)的功能,并對(duì)比分析了1.5與1.0設(shè)置Service的技巧,需要的朋友可以參考下
    2016-06-06
  • Android 7.0 SEAndroid app權(quán)限配置方法

    Android 7.0 SEAndroid app權(quán)限配置方法

    今天小編就為大家分享一篇Android 7.0 SEAndroid app權(quán)限配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Android ScreenLockReceiver監(jiān)聽鎖屏功能示例

    Android ScreenLockReceiver監(jiān)聽鎖屏功能示例

    這篇文章主要介紹了Android ScreenLockReceiver監(jiān)聽鎖屏功能,結(jié)合實(shí)例形式分析了Android監(jiān)聽鎖屏及取消監(jiān)聽功能的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • Android高仿2048小游戲?qū)崿F(xiàn)代碼

    Android高仿2048小游戲?qū)崿F(xiàn)代碼

    這篇文章主要介紹了Android高仿2048小游戲?qū)崿F(xiàn)代碼的相關(guān)資料,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10
  • Android如何獲取子View的位置及坐標(biāo)詳解

    Android如何獲取子View的位置及坐標(biāo)詳解

    這篇文章主要給大家介紹了關(guān)于Android如何獲取子View的位置及坐標(biāo)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁(yè)源碼查看器

    Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁(yè)源碼查看器

    本篇文章主要介紹了Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁(yè)源碼查看器的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能

    android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • android實(shí)現(xiàn)貝塞爾曲線之波浪效果

    android實(shí)現(xiàn)貝塞爾曲線之波浪效果

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)貝塞爾曲線之波浪效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android實(shí)現(xiàn)自定義華麗的水波紋效果

    Android實(shí)現(xiàn)自定義華麗的水波紋效果

    關(guān)于Android的水波紋效果小編之前給大家也分享幾篇類似的,有興趣可通過(guò)下面的相關(guān)文章進(jìn)行查看,今天給大家再分享一個(gè)華麗的水波紋效果,這個(gè)效果很不錯(cuò),感興趣的可以參考借鑒。
    2016-08-08

最新評(píng)論

长阳| 岱山县| 苏尼特右旗| 长乐市| 五峰| 图木舒克市| 西昌市| 宁国市| 建宁县| 启东市| 新蔡县| 贺州市| 阜平县| 三亚市| 花垣县| 龙胜| 枣阳市| 灵山县| 台安县| 丹巴县| 辽中县| 旌德县| 高安市| 海伦市| 昌平区| 龙岩市| 南木林县| 二连浩特市| 玛多县| 吉木乃县| 屯昌县| 乐业县| 萨迦县| 西乌| 沾益县| 哈密市| 宣威市| 沙湾县| 全南县| 枣阳市| 郁南县|