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

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

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

本文實例為大家分享了android實現(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中實例化ITelephony,并返回 
  */ 
 static public ITelephony getITelephony(TelephonyManager telMgr) 
   throws Exception { 
  Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod( 
    "getITelephony"); 
  getITelephonyMethod.setAccessible(true);// 私有化函數(shù)也能使用 
  return (ITelephony) getITelephonyMethod.invoke(telMgr); 
 } 
  
 //自動接聽 
 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); 
   } 
  } 
 } 
  
 //自動掛斷 
 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:// 電話打進來接通狀態(tài);電話打出時首先監(jiān)聽到的狀態(tài)。 
   Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK"); 
   break; 
  case TelephonyManager.CALL_STATE_RINGING:// 電話打進來狀態(tài) 
   Log.i("onCallStateChanged", "CALL_STATE_RINGING"); 
   PhoneUtils.autoAnswerPhone(context,tm); 
   break; 
  case TelephonyManager.CALL_STATE_IDLE:// 不管是電話打出去還是電話打進來都會監(jiān)聽到的狀態(tài)。 
   Log.i("onCallStateChanged", "CALL_STATE_IDLE"); 
   break; 
  } 
 } 
 
  
} 

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

最后別忘記在AndroidManifest.xml里聲明和注冊權(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> 

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

相關(guān)文章

  • Android編程實現(xiàn)簡單文件瀏覽器功能

    Android編程實現(xiàn)簡單文件瀏覽器功能

    這篇文章主要介紹了Android編程實現(xiàn)簡單文件瀏覽器功能,結(jié)合實例形式分析了Android文件管理器的布局、文件與目錄的遍歷、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • 將Eclipse工程轉(zhuǎn)Android Studio工程的步驟與注意事項

    將Eclipse工程轉(zhuǎn)Android Studio工程的步驟與注意事項

    這篇文章主要給大家介紹了將Eclipse工程轉(zhuǎn)Android Studio工程的方法步驟,并給大家分享了其中的一些注意事項,文中將實現(xiàn)的步驟一步步介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Java中的final關(guān)鍵字詳解及實例

    Java中的final關(guān)鍵字詳解及實例

    這篇文章主要介紹了Java中的final關(guān)鍵字詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 詳解VirtualApk啟動插件Activity

    詳解VirtualApk啟動插件Activity

    這篇文章主要介紹了詳解VirtualApk啟動插件Activity,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • android中的AIDL進程間通信示例

    android中的AIDL進程間通信示例

    進程之間不能共享內(nèi)存,那么怎么在不同的應(yīng)用程序中進行通訊,這就要依賴AIDL機制,本文詳細(xì)介紹了android中的AIDL進程間通信示例,有興趣的可以了解一下。
    2016-11-11
  • 安卓模擬器genymotion的安裝與使用圖文教程

    安卓模擬器genymotion的安裝與使用圖文教程

    這篇文章主要為大家詳細(xì)介紹了安卓模擬器genymotion的安裝與使用圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android仿微信主界面設(shè)計

    Android仿微信主界面設(shè)計

    這篇文章主要為大家介紹了Android仿微信主界面設(shè)計的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android自定義View實現(xiàn)分段選擇按鈕的實現(xiàn)代碼

    Android自定義View實現(xiàn)分段選擇按鈕的實現(xiàn)代碼

    這篇文章主要介紹了Android自定義View實現(xiàn)分段選擇按鈕的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Android中的binder機制詳解

    Android中的binder機制詳解

    這篇文章主要介紹了Android中的binder機制詳解,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android 截取手機屏幕兩種實現(xiàn)方法

    Android 截取手機屏幕兩種實現(xiàn)方法

    這篇文章主要介紹了Android 截取手機屏幕兩種實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評論