Android拒绝来电的实现--ITelephony类的反射(结束电话)
16lz
2021-12-04
NeighboringCellInfo.aidl
view plain print ?- packageandroid.telephony;
- parcelableNeighboringCellInfo;
ITelephony.aidl
view plain print ?- /*
- *Copyright(C)2007TheAndroidOpenSourceProject
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- packagecom.android.internal.telephony;
- importandroid.os.Bundle;
- importjava.util.List;
- importandroid.telephony.NeighboringCellInfo;
- /**
- *Interfaceusedtointeractwiththephone.Mostlythisisusedbythe
- *TelephonyManagerclass.Afewplacesarestillusingthisdirectly.
- *PleasecleanthemupifpossibleanduseTelephonyManagerinsteadl.
- *
- *{@hide}
- */
- interfaceITelephony{
- /**
- *Dialanumber.Thisdoesn'tplacethecall.Itdisplays
- *theDialerscreen.
- *@paramnumberthenumbertobedialed.Ifnull,this
- *woulddisplaytheDialerscreenwithnonumberpre-filled.
- */
- voiddial(Stringnumber);
- /**
- *Placeacalltothespecifiednumber.
- *@paramnumberthenumbertobecalled.
- */
- voidcall(Stringnumber);
- /**
- *Ifthereiscurrentlyacallinprogress,showthecallscreen.
- *TheDTMFdialpadmayormaynotbevisibleinitially,dependingon
- *whetheritwasupwhentheuserlastexitedtheInCallScreen.
- *
- *@returntrueifthecallscreenwasshown.
- */
- booleanshowCallScreen();
- /**
- *VariationofshowCallScreen()thatalsospecifieswhetherthe
- *DTMFdialpadshouldbeinitiallyvisiblewhentheInCallScreen
- *comesup.
- *
- *@paramshowDialpadiftrue,makethedialpadvisibleinitially,
- *otherwisehidethedialpadinitially.
- *@returntrueifthecallscreenwasshown.
- *
- *@seeshowCallScreen
- */
- booleanshowCallScreenWithDialpad(booleanshowDialpad);
- /**
- *EndcallorgototheHomescreen
- *
- *@returnwhetherithungup
- */
- booleanendCall();
- /**
- *Answerthecurrently-ringingcall.
- *
- *Ifthere'salreadyacurrentactivecall,thatcallwillbe
- *automaticallyputonhold.Ifbothlinesarecurrentlyinuse,the
- *currentactivecallwillbeended.
- *
- *TODO:provideaflagtoletthecallerspecifywhatpolicytouse
- *ifbothlinesareinuse.(Thecurrentbehaviorishardwiredto
- *"answerincoming,endongoing",whichishowtheCALLbutton
- *isspeccedtobehave.)
- *
- *TODO:thisshouldbeaonewaycall(especiallysinceit'scalled
- *directlyfromthekeyqueuethread).
- */
- voidanswerRingingCall();
- /**
- *Silencetheringerifanincomingcalliscurrentlyringing.
- *(Ifvibrating,stopthevibratoralso.)
- *
- *It'ssafetocallthisiftheringerhasalreadybeensilenced,or
- *evenifthere'snoincomingcall.(Ifso,thismethodwilldonothing.)
- *
- *TODO:thisshouldbeaonewaycalltoo(seeabove).
- *(Actually*all*themethodsherethatreturnvoidcan
- *probablybeoneway.)
- */
- voidsilenceRinger();
- /**
- *Checkifweareineitheranactiveorholdingcall
- *@returntrueifthephonestateisOFFHOOK.
- */
- booleanisOffhook();
- /**
- *Checkifanincomingphonecallisringingorcallwaiting.
- *@returntrueifthephonestateisRINGING.
- */
- booleanisRinging();
- /**
- *Checkifthephoneisidle.
- *@returntrueifthephonestateisIDLE.
- */
- booleanisIdle();
- /**
- *Checktoseeiftheradioisonornot.
- *@returnreturnstrueiftheradioison.
- */
- booleanisRadioOn();
- /**
- *CheckiftheSIMpinlockisenabled.
- *@returntrueiftheSIMpinlockisenabled.
- */
- booleanisSimPinEnabled();
- /**
- *Cancelsthemissedcallsnotification.
- */
- voidcancelMissedCallsNotification();
- /**
- *SupplyapintounlocktheSIM.Blocksuntilaresultisdetermined.
- *@parampinThepintocheck.
- *@returnwhethertheoperationwasasuccess.
- */
- booleansupplyPin(Stringpin);
- /**
- *HandlesPINMMIcommands(PIN/PIN2/PUK/PUK2),whichareinitiated
- *withoutSEND(so<code>dial</code>isnotappropriate).
- *
- *@paramdialStringtheMMIcommandtobeexecuted.
- *@returntrueifMMIcommandisexecuted.
- */
- booleanhandlePinMmi(StringdialString);
- /**
- *Togglestheradioonoroff.
- */
- voidtoggleRadioOnOff();
- /**
- *Settheradiotoonoroff
- */
- booleansetRadio(booleanturnOn);
- /**
- *Requesttoupdatelocationinformationinservicestate
- */
- voidupdateServiceLocation();
- /**
- *Enablelocationupdatenotifications.
- */
- voidenableLocationUpdates();
- /**
- *Disablelocationupdatenotifications.
- */
- voiddisableLocationUpdates();
- /**
- *EnableaspecificAPNtype.
- */
- intenableApnType(Stringtype);
- /**
- *DisableaspecificAPNtype.
- */
- intdisableApnType(Stringtype);
- /**
- *Allowmobiledataconnections.
- */
- booleanenableDataConnectivity();
- /**
- *Disallowmobiledataconnections.
- */
- booleandisableDataConnectivity();
- /**
- *Reportwhetherdataconnectivityispossible.
- */
- booleanisDataConnectivityPossible();
- BundlegetCellLocation();
- /**
- *Returnstheneighboringcellinformationofthedevice.
- */
- List<NeighboringCellInfo>getNeighboringCellInfo();
- intgetCallState();
- intgetDataActivity();
- intgetDataState();
- /**
- *Returnsthecurrentactivephonetypeasinteger.
- *ReturnsTelephonyManager.PHONE_TYPE_CDMAifRILConstants.CDMA_PHONE
- *andTelephonyManager.PHONE_TYPE_GSMifRILConstants.GSM_PHONE
- */
- intgetActivePhoneType();
- /**
- *ReturnstheCDMAERIiconindextodisplay
- */
- intgetCdmaEriIconIndex();
- /**
- *ReturnstheCDMAERIiconmode,
- *0-ON
- *1-FLASHING
- */
- intgetCdmaEriIconMode();
- /**
- *ReturnstheCDMAERItext,
- */
- StringgetCdmaEriText();
- /**
- *ReturnstrueifCDMAprovisioningneedstorun.
- */
- booleangetCdmaNeedsProvisioning();
- /**
- *Returnstheunreadcountofvoicemails
- */
- intgetVoiceMessageCount();
- /**
- *Returnsthenetworktype
- */
- intgetNetworkType();
- /**
- *ReturntrueifanICCcardispresent
- */
- booleanhasIccCard();
- }
PhoneCallActivity .java
view plain print ?- packagecom.geolo.pcp.android;
- importjava.lang.reflect.Method;
- importcom.android.internal.telephony.ITelephony;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.media.AudioManager;
- importandroid.os.Bundle;
- importandroid.telephony.PhoneStateListener;
- importandroid.telephony.ServiceState;
- importandroid.telephony.SignalStrength;
- importandroid.telephony.TelephonyManager;
- importandroid.util.Log;
- publicclassPhoneCallActivityextendsActivity{
- privatePhoneCallListenermPhoneCallListener;
- privateTelephonyManagermTelephonyManager;
- //privatePhoneStateListenermPhoneStateListener;
- privateAudioManagermAudioManager;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- mPhoneCallListener=newPhoneCallListener();
- mTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
- mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_CALL_STATE);
- //mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_SERVICE_STATE);
- //mTelephonyManager.listen(mPhoneCallListener,PhoneCallListener.LISTEN_DATA_CONNECTION_STATE);
- }
- /**
- *利用JAVA反射机制调用ITelephony的endCall()结束通话。
- */
- privatevoidendCall(){
- //初始化iTelephony
- Class<TelephonyManager>c=TelephonyManager.class;
- MethodgetITelephonyMethod=null;
- try{
- //获取所有public/private/protected/默认
- //方法的函数,如果只需要获取public方法,则可以调用getMethod.
- getITelephonyMethod=c.getDeclaredMethod("getITelephony",(Class[])null);
- //将要执行的方法对象设置是否进行访问检查,也就是说对于public/private/protected/默认
- //我们是否能够访问。值为true则指示反射的对象在使用时应该取消Java语言访问检查。值为false
- //则指示反射的对象应该实施Java语言访问检查。
- getITelephonyMethod.setAccessible(true);
- ITelephonyiTelephony=(ITelephony)getITelephonyMethod.invoke(mTelephonyManager,(Object[])null);
- iTelephony.endCall();
- Log.v(this.getClass().getName(),"endCall......");
- }catch(Exceptione){
- Log.e(this.getClass().getName(),"endCallError",e);
- }
- }
- publicclassPhoneCallListenerextendsPhoneStateListener{
- @Override
- publicvoidonCallStateChanged(intstate,StringincomingNumber){
- mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
- Log.v(this.getClass().getName(),"onCallStateChanged-state:"+state);
- Log.v(this.getClass().getName(),"onCallStateChanged-incomingNumber:"+incomingNumber);
- switch(state){
- caseTelephonyManager.CALL_STATE_IDLE:
- mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
- break;
- caseTelephonyManager.CALL_STATE_OFFHOOK:
- //CALL_STATE_OFFHOOK;
- break;
- caseTelephonyManager.CALL_STATE_RINGING:
- if("123456".equals(incomingNumber)){
- //mTelephonyService.endCall();
- endCall();
- }else{
- mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
- }
- break;
- default:
- break;
- }
- super.onCallStateChanged(state,incomingNumber);
- }
- @Override
- publicvoidonDataConnectionStateChanged(intstate){
- Log.v(this.getClass().getName(),"onDataConnectionStateChanged-state:"+state);
- super.onDataConnectionStateChanged(state);
- }
- @Override
- publicvoidonDataConnectionStateChanged(intstate,intnetworkType){
- Log.v(this.getClass().getName(),"onDataConnectionStateChanged-state:"+state);
- Log.v(this.getClass().getName(),"onDataConnectionStateChanged-networkType:"+networkType);
- super.onDataConnectionStateChanged(state,networkType);
- }
- @Override
- publicvoidonServiceStateChanged(ServiceStateserviceState){
- Log.v(this.getClass().getName(),"onServiceStateChanged-ServiceState:"+serviceState);
- super.onServiceStateChanged(serviceState);
- }
- @Override
- publicvoidonSignalStrengthChanged(intasu){
- Log.v(this.getClass().getName(),"onSignalStrengthChanged-asu:"+asu);
- super.onSignalStrengthChanged(asu);
- }
- @Override
- publicvoidonSignalStrengthsChanged(SignalStrengthsignalStrength){
- Log.v(this.getClass().getName(),"onSignalStrengthsChanged-signalStrength:"+signalStrength);
- super.onSignalStrengthsChanged(signalStrength);
- }
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.geolo.pcp.android"android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".PhoneCallActivity"android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
- <uses-permissionandroid:name="android.permission.CALL_PHONE"/>
- <uses-permissionandroid:name="android.permission.MODIFY_PHONE_STATE"/>
- <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
- <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- <uses-permissionandroid:name="android.permission.WAKE_LOCK"></uses-permission>
- </manifest>
原理请看:http://blog.csdn.net/GEOLO/archive/2010/12/21/6090264.aspx
项目源码:http://download.csdn.net/source/2927291
更多相关文章
- android 中常用的权限
- 用Javascript判断访问来源操作系统, 设备, 浏览器类型
- Android(安卓)SDK的docs访问速度很慢
- android viewPage 判断是否到最后一页
- Android(安卓)WebView访问SSL证书网页(onReceivedSslError)
- android下的android.os.StrictMode$AndroidBlockGuardPolicy.onN
- Android菜鸟日记14-对话框
- Android(安卓)ListView强制停止滚动
- android 不自动弹出虚拟键盘