Android拒绝来电的实现--ITelephony类的反射(结束电话)_第1张图片

NeighboringCellInfo.aidl

view plain print ?
  1. packageandroid.telephony;
  2. parcelableNeighboringCellInfo;

ITelephony.aidl

view plain print ?
  1. /*
  2. *Copyright(C)2007TheAndroidOpenSourceProject
  3. *
  4. *LicensedundertheApacheLicense,Version2.0(the"License");
  5. *youmaynotusethisfileexceptincompliancewiththeLicense.
  6. *YoumayobtainacopyoftheLicenseat
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  11. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  12. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  14. *limitationsundertheLicense.
  15. */
  16. packagecom.android.internal.telephony;
  17. importandroid.os.Bundle;
  18. importjava.util.List;
  19. importandroid.telephony.NeighboringCellInfo;
  20. /**
  21. *Interfaceusedtointeractwiththephone.Mostlythisisusedbythe
  22. *TelephonyManagerclass.Afewplacesarestillusingthisdirectly.
  23. *PleasecleanthemupifpossibleanduseTelephonyManagerinsteadl.
  24. *
  25. *{@hide}
  26. */
  27. interfaceITelephony{
  28. /**
  29. *Dialanumber.Thisdoesn'tplacethecall.Itdisplays
  30. *theDialerscreen.
  31. *@paramnumberthenumbertobedialed.Ifnull,this
  32. *woulddisplaytheDialerscreenwithnonumberpre-filled.
  33. */
  34. voiddial(Stringnumber);
  35. /**
  36. *Placeacalltothespecifiednumber.
  37. *@paramnumberthenumbertobecalled.
  38. */
  39. voidcall(Stringnumber);
  40. /**
  41. *Ifthereiscurrentlyacallinprogress,showthecallscreen.
  42. *TheDTMFdialpadmayormaynotbevisibleinitially,dependingon
  43. *whetheritwasupwhentheuserlastexitedtheInCallScreen.
  44. *
  45. *@returntrueifthecallscreenwasshown.
  46. */
  47. booleanshowCallScreen();
  48. /**
  49. *VariationofshowCallScreen()thatalsospecifieswhetherthe
  50. *DTMFdialpadshouldbeinitiallyvisiblewhentheInCallScreen
  51. *comesup.
  52. *
  53. *@paramshowDialpadiftrue,makethedialpadvisibleinitially,
  54. *otherwisehidethedialpadinitially.
  55. *@returntrueifthecallscreenwasshown.
  56. *
  57. *@seeshowCallScreen
  58. */
  59. booleanshowCallScreenWithDialpad(booleanshowDialpad);
  60. /**
  61. *EndcallorgototheHomescreen
  62. *
  63. *@returnwhetherithungup
  64. */
  65. booleanendCall();
  66. /**
  67. *Answerthecurrently-ringingcall.
  68. *
  69. *Ifthere'salreadyacurrentactivecall,thatcallwillbe
  70. *automaticallyputonhold.Ifbothlinesarecurrentlyinuse,the
  71. *currentactivecallwillbeended.
  72. *
  73. *TODO:provideaflagtoletthecallerspecifywhatpolicytouse
  74. *ifbothlinesareinuse.(Thecurrentbehaviorishardwiredto
  75. *"answerincoming,endongoing",whichishowtheCALLbutton
  76. *isspeccedtobehave.)
  77. *
  78. *TODO:thisshouldbeaonewaycall(especiallysinceit'scalled
  79. *directlyfromthekeyqueuethread).
  80. */
  81. voidanswerRingingCall();
  82. /**
  83. *Silencetheringerifanincomingcalliscurrentlyringing.
  84. *(Ifvibrating,stopthevibratoralso.)
  85. *
  86. *It'ssafetocallthisiftheringerhasalreadybeensilenced,or
  87. *evenifthere'snoincomingcall.(Ifso,thismethodwilldonothing.)
  88. *
  89. *TODO:thisshouldbeaonewaycalltoo(seeabove).
  90. *(Actually*all*themethodsherethatreturnvoidcan
  91. *probablybeoneway.)
  92. */
  93. voidsilenceRinger();
  94. /**
  95. *Checkifweareineitheranactiveorholdingcall
  96. *@returntrueifthephonestateisOFFHOOK.
  97. */
  98. booleanisOffhook();
  99. /**
  100. *Checkifanincomingphonecallisringingorcallwaiting.
  101. *@returntrueifthephonestateisRINGING.
  102. */
  103. booleanisRinging();
  104. /**
  105. *Checkifthephoneisidle.
  106. *@returntrueifthephonestateisIDLE.
  107. */
  108. booleanisIdle();
  109. /**
  110. *Checktoseeiftheradioisonornot.
  111. *@returnreturnstrueiftheradioison.
  112. */
  113. booleanisRadioOn();
  114. /**
  115. *CheckiftheSIMpinlockisenabled.
  116. *@returntrueiftheSIMpinlockisenabled.
  117. */
  118. booleanisSimPinEnabled();
  119. /**
  120. *Cancelsthemissedcallsnotification.
  121. */
  122. voidcancelMissedCallsNotification();
  123. /**
  124. *SupplyapintounlocktheSIM.Blocksuntilaresultisdetermined.
  125. *@parampinThepintocheck.
  126. *@returnwhethertheoperationwasasuccess.
  127. */
  128. booleansupplyPin(Stringpin);
  129. /**
  130. *HandlesPINMMIcommands(PIN/PIN2/PUK/PUK2),whichareinitiated
  131. *withoutSEND(so<code>dial</code>isnotappropriate).
  132. *
  133. *@paramdialStringtheMMIcommandtobeexecuted.
  134. *@returntrueifMMIcommandisexecuted.
  135. */
  136. booleanhandlePinMmi(StringdialString);
  137. /**
  138. *Togglestheradioonoroff.
  139. */
  140. voidtoggleRadioOnOff();
  141. /**
  142. *Settheradiotoonoroff
  143. */
  144. booleansetRadio(booleanturnOn);
  145. /**
  146. *Requesttoupdatelocationinformationinservicestate
  147. */
  148. voidupdateServiceLocation();
  149. /**
  150. *Enablelocationupdatenotifications.
  151. */
  152. voidenableLocationUpdates();
  153. /**
  154. *Disablelocationupdatenotifications.
  155. */
  156. voiddisableLocationUpdates();
  157. /**
  158. *EnableaspecificAPNtype.
  159. */
  160. intenableApnType(Stringtype);
  161. /**
  162. *DisableaspecificAPNtype.
  163. */
  164. intdisableApnType(Stringtype);
  165. /**
  166. *Allowmobiledataconnections.
  167. */
  168. booleanenableDataConnectivity();
  169. /**
  170. *Disallowmobiledataconnections.
  171. */
  172. booleandisableDataConnectivity();
  173. /**
  174. *Reportwhetherdataconnectivityispossible.
  175. */
  176. booleanisDataConnectivityPossible();
  177. BundlegetCellLocation();
  178. /**
  179. *Returnstheneighboringcellinformationofthedevice.
  180. */
  181. List<NeighboringCellInfo>getNeighboringCellInfo();
  182. intgetCallState();
  183. intgetDataActivity();
  184. intgetDataState();
  185. /**
  186. *Returnsthecurrentactivephonetypeasinteger.
  187. *ReturnsTelephonyManager.PHONE_TYPE_CDMAifRILConstants.CDMA_PHONE
  188. *andTelephonyManager.PHONE_TYPE_GSMifRILConstants.GSM_PHONE
  189. */
  190. intgetActivePhoneType();
  191. /**
  192. *ReturnstheCDMAERIiconindextodisplay
  193. */
  194. intgetCdmaEriIconIndex();
  195. /**
  196. *ReturnstheCDMAERIiconmode,
  197. *0-ON
  198. *1-FLASHING
  199. */
  200. intgetCdmaEriIconMode();
  201. /**
  202. *ReturnstheCDMAERItext,
  203. */
  204. StringgetCdmaEriText();
  205. /**
  206. *ReturnstrueifCDMAprovisioningneedstorun.
  207. */
  208. booleangetCdmaNeedsProvisioning();
  209. /**
  210. *Returnstheunreadcountofvoicemails
  211. */
  212. intgetVoiceMessageCount();
  213. /**
  214. *Returnsthenetworktype
  215. */
  216. intgetNetworkType();
  217. /**
  218. *ReturntrueifanICCcardispresent
  219. */
  220. booleanhasIccCard();
  221. }

PhoneCallActivity .java

view plain print ?
  1. packagecom.geolo.pcp.android;
  2. importjava.lang.reflect.Method;
  3. importcom.android.internal.telephony.ITelephony;
  4. importandroid.app.Activity;
  5. importandroid.content.Context;
  6. importandroid.media.AudioManager;
  7. importandroid.os.Bundle;
  8. importandroid.telephony.PhoneStateListener;
  9. importandroid.telephony.ServiceState;
  10. importandroid.telephony.SignalStrength;
  11. importandroid.telephony.TelephonyManager;
  12. importandroid.util.Log;
  13. publicclassPhoneCallActivityextendsActivity{
  14. privatePhoneCallListenermPhoneCallListener;
  15. privateTelephonyManagermTelephonyManager;
  16. //privatePhoneStateListenermPhoneStateListener;
  17. privateAudioManagermAudioManager;
  18. @Override
  19. protectedvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. mPhoneCallListener=newPhoneCallListener();
  22. mTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  23. mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
  24. mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_CALL_STATE);
  25. //mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_SERVICE_STATE);
  26. //mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_DATA_CONNECTION_STATE);
  27. }
  28. /**
  29. *利用JAVA反射机制调用ITelephony的endCall()结束通话。
  30. */
  31. privatevoidendCall(){
  32. //初始化iTelephony
  33. Class<TelephonyManager>c=TelephonyManager.class;
  34. MethodgetITelephonyMethod=null;
  35. try{
  36. //获取所有public/private/protected/默认
  37. //方法的函数,如果只需要获取public方法,则可以调用getMethod.
  38. getITelephonyMethod=c.getDeclaredMethod("getITelephony",(Class[])null);
  39. //将要执行的方法对象设置是否进行访问检查,也就是说对于public/private/protected/默认
  40. //我们是否能够访问。值为true则指示反射的对象在使用时应该取消Java语言访问检查。值为false
  41. //则指示反射的对象应该实施Java语言访问检查。
  42. getITelephonyMethod.setAccessible(true);
  43. ITelephonyiTelephony=(ITelephony)getITelephonyMethod.invoke(mTelephonyManager,(Object[])null);
  44. iTelephony.endCall();
  45. Log.v(this.getClass().getName(),"endCall......");
  46. }catch(Exceptione){
  47. Log.e(this.getClass().getName(),"endCallError",e);
  48. }
  49. }
  50. publicclassPhoneCallListenerextendsPhoneStateListener{
  51. @Override
  52. publicvoidonCallStateChanged(intstate,StringincomingNumber){
  53. mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
  54. Log.v(this.getClass().getName(),"onCallStateChanged-state:"+state);
  55. Log.v(this.getClass().getName(),"onCallStateChanged-incomingNumber:"+incomingNumber);
  56. switch(state){
  57. caseTelephonyManager.CALL_STATE_IDLE:
  58. mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
  59. break;
  60. caseTelephonyManager.CALL_STATE_OFFHOOK:
  61. //CALL_STATE_OFFHOOK;
  62. break;
  63. caseTelephonyManager.CALL_STATE_RINGING:
  64. if("123456".equals(incomingNumber)){
  65. //mTelephonyService.endCall();
  66. endCall();
  67. }else{
  68. mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. super.onCallStateChanged(state,incomingNumber);
  75. }
  76. @Override
  77. publicvoidonDataConnectionStateChanged(intstate){
  78. Log.v(this.getClass().getName(),"onDataConnectionStateChanged-state:"+state);
  79. super.onDataConnectionStateChanged(state);
  80. }
  81. @Override
  82. publicvoidonDataConnectionStateChanged(intstate,intnetworkType){
  83. Log.v(this.getClass().getName(),"onDataConnectionStateChanged-state:"+state);
  84. Log.v(this.getClass().getName(),"onDataConnectionStateChanged-networkType:"+networkType);
  85. super.onDataConnectionStateChanged(state,networkType);
  86. }
  87. @Override
  88. publicvoidonServiceStateChanged(ServiceStateserviceState){
  89. Log.v(this.getClass().getName(),"onServiceStateChanged-ServiceState:"+serviceState);
  90. super.onServiceStateChanged(serviceState);
  91. }
  92. @Override
  93. publicvoidonSignalStrengthChanged(intasu){
  94. Log.v(this.getClass().getName(),"onSignalStrengthChanged-asu:"+asu);
  95. super.onSignalStrengthChanged(asu);
  96. }
  97. @Override
  98. publicvoidonSignalStrengthsChanged(SignalStrengthsignalStrength){
  99. Log.v(this.getClass().getName(),"onSignalStrengthsChanged-signalStrength:"+signalStrength);
  100. super.onSignalStrengthsChanged(signalStrength);
  101. }
  102. }
  103. }

view plain print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.geolo.pcp.android"android:versionCode="1"
  4. android:versionName="1.0">
  5. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  6. <activityandroid:name=".PhoneCallActivity"android:label="@string/app_name">
  7. <intent-filter>
  8. <actionandroid:name="android.intent.action.MAIN"/>
  9. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  10. </intent-filter>
  11. </activity>
  12. </application>
  13. <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
  14. <uses-permissionandroid:name="android.permission.CALL_PHONE"/>
  15. <uses-permissionandroid:name="android.permission.MODIFY_PHONE_STATE"/>
  16. <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  17. <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  18. <uses-permissionandroid:name="android.permission.WAKE_LOCK"></uses-permission>
  19. </manifest>

原理请看:http://blog.csdn.net/GEOLO/archive/2010/12/21/6090264.aspx

项目源码:http://download.csdn.net/source/2927291

更多相关文章

  1. Android WebView中的JavaScript调用android方法
  2. [导入]Android平台上四种保存数据的方法
  3. Android的关键的持久数据应该在onPause()方法中保存
  4. Android添加单元测试的方法与步骤
  5. Android Studio无法执行Java类的main方法问题及解决方法
  6. Android获取屏幕宽度的4种方法
  7. android 开发收银系统,弹出键盘固定界面UI的方法
  8. Android优质学习方法
  9. Android TextView各种使用方法

随机推荐

  1. Android 使用ORMLite打造万能泛型Dao简化
  2. 1.Android(了解Android王国)
  3. Service和Activity通讯的3种常用方式示例
  4. android tab和listiew
  5. Android(安卓)自动检测版本并升级
  6. android中资源整理
  7. Android(安卓)架构组件(一)——Lifecycle
  8. Afinal框架(简介)
  9. 属性资源与Android命名空间
  10. Android中生成和扫描二维码