Android拒绝来电的实现--ITelephony类的反射

原文地址:http://blog.csdn.net/geolo/article/details/6090303


NeighboringCellInfo.aidl

[java]  view plain copy print ?
  1. package android.telephony;  
  2. parcelable NeighboringCellInfo;  
 

ITelephony.aidl

[java]  view plain copy print ?
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16. package com.android.internal.telephony;  
  17. import android.os.Bundle;  
  18. import java.util.List;  
  19. import android.telephony.NeighboringCellInfo;  
  20. /** 
  21.  * Interface used to interact with the phone.  Mostly this is used by the 
  22.  * TelephonyManager class.  A few places are still using this directly. 
  23.  * Please clean them up if possible and use TelephonyManager insteadl. 
  24.  * 
  25.  * {@hide} 
  26.  */  
  27. interface ITelephony {  
  28.     /** 
  29.      * Dial a number. This doesn't place the call. It displays 
  30.      * the Dialer screen. 
  31.      * @param number the number to be dialed. If null, this 
  32.      * would display the Dialer screen with no number pre-filled. 
  33.      */  
  34.     void dial(String number);  
  35.     /** 
  36.      * Place a call to the specified number. 
  37.      * @param number the number to be called. 
  38.      */  
  39.     void call(String number);  
  40.     /** 
  41.      * If there is currently a call in progress, show the call screen. 
  42.      * The DTMF dialpad may or may not be visible initially, depending on 
  43.      * whether it was up when the user last exited the InCallScreen. 
  44.      * 
  45.      * @return true if the call screen was shown. 
  46.      */  
  47.     boolean showCallScreen();  
  48.     /** 
  49.      * Variation of showCallScreen() that also specifies whether the 
  50.      * DTMF dialpad should be initially visible when the InCallScreen 
  51.      * comes up. 
  52.      * 
  53.      * @param showDialpad if true, make the dialpad visible initially, 
  54.      *                    otherwise hide the dialpad initially. 
  55.      * @return true if the call screen was shown. 
  56.      * 
  57.      * @see showCallScreen 
  58.      */  
  59.     boolean showCallScreenWithDialpad(boolean showDialpad);  
  60.     /** 
  61.      * End call or go to the Home screen 
  62.      * 
  63.      * @return whether it hung up 
  64.      */  
  65.     boolean endCall();  
  66.     /** 
  67.      * Answer the currently-ringing call. 
  68.      * 
  69.      * If there's already a current active call, that call will be 
  70.      * automatically put on hold.  If both lines are currently in use, the 
  71.      * current active call will be ended. 
  72.      * 
  73.      * TODO: provide a flag to let the caller specify what policy to use 
  74.      * if both lines are in use.  (The current behavior is hardwired to 
  75.      * "answer incoming, end ongoing", which is how the CALL button 
  76.      * is specced to behave.) 
  77.      * 
  78.      * TODO: this should be a oneway call (especially since it's called 
  79.      * directly from the key queue thread). 
  80.      */  
  81.     void answerRingingCall();  
  82.     /** 
  83.      * Silence the ringer if an incoming call is currently ringing. 
  84.      * (If vibrating, stop the vibrator also.) 
  85.      * 
  86.      * It's safe to call this if the ringer has already been silenced, or 
  87.      * even if there's no incoming call.  (If so, this method will do nothing.) 
  88.      * 
  89.      * TODO: this should be a oneway call too (see above). 
  90.      *       (Actually *all* the methods here that return void can 
  91.      *       probably be oneway.) 
  92.      */  
  93.     void silenceRinger();  
  94.     /** 
  95.      * Check if we are in either an active or holding call 
  96.      * @return true if the phone state is OFFHOOK. 
  97.      */  
  98.     boolean isOffhook();  
  99.     /** 
  100.      * Check if an incoming phone call is ringing or call waiting. 
  101.      * @return true if the phone state is RINGING. 
  102.      */  
  103.     boolean isRinging();  
  104.     /** 
  105.      * Check if the phone is idle. 
  106.      * @return true if the phone state is IDLE. 
  107.      */  
  108.     boolean isIdle();  
  109.     /** 
  110.      * Check to see if the radio is on or not. 
  111.      * @return returns true if the radio is on. 
  112.      */  
  113.     boolean isRadioOn();  
  114.     /** 
  115.      * Check if the SIM pin lock is enabled. 
  116.      * @return true if the SIM pin lock is enabled. 
  117.      */  
  118.     boolean isSimPinEnabled();  
  119.     /** 
  120.      * Cancels the missed calls notification. 
  121.      */  
  122.     void cancelMissedCallsNotification();  
  123.     /** 
  124.      * Supply a pin to unlock the SIM.  Blocks until a result is determined. 
  125.      * @param pin The pin to check. 
  126.      * @return whether the operation was a success. 
  127.      */  
  128.     boolean supplyPin(String pin);  
  129.     /** 
  130.      * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which are initiated 
  131.      * without SEND (so <code>dial</code> is not appropriate). 
  132.      * 
  133.      * @param dialString the MMI command to be executed. 
  134.      * @return true if MMI command is executed. 
  135.      */  
  136.     boolean handlePinMmi(String dialString);  
  137.     /** 
  138.      * Toggles the radio on or off. 
  139.      */  
  140.     void toggleRadioOnOff();  
  141.     /** 
  142.      * Set the radio to on or off 
  143.      */  
  144.     boolean setRadio(boolean turnOn);  
  145.     /** 
  146.      * Request to update location information in service state 
  147.      */  
  148.     void updateServiceLocation();  
  149.     /** 
  150.      * Enable location update notifications. 
  151.      */  
  152.     void enableLocationUpdates();  
  153.     /** 
  154.      * Disable location update notifications. 
  155.      */  
  156.     void disableLocationUpdates();  
  157.     /** 
  158.      * Enable a specific APN type. 
  159.      */  
  160.     int enableApnType(String type);  
  161.     /** 
  162.      * Disable a specific APN type. 
  163.      */  
  164.     int disableApnType(String type);  
  165.     /** 
  166.      * Allow mobile data connections. 
  167.      */  
  168.     boolean enableDataConnectivity();  
  169.     /** 
  170.      * Disallow mobile data connections. 
  171.      */  
  172.     boolean disableDataConnectivity();  
  173.     /** 
  174.      * Report whether data connectivity is possible. 
  175.      */  
  176.     boolean isDataConnectivityPossible();  
  177.     Bundle getCellLocation();  
  178.     /** 
  179.      * Returns the neighboring cell information of the device. 
  180.      */  
  181.     List<NeighboringCellInfo> getNeighboringCellInfo();  
  182.      int getCallState();  
  183.      int getDataActivity();  
  184.      int getDataState();  
  185.     /** 
  186.      * Returns the current active phone type as integer. 
  187.      * Returns TelephonyManager.PHONE_TYPE_CDMA if RILConstants.CDMA_PHONE 
  188.      * and TelephonyManager.PHONE_TYPE_GSM if RILConstants.GSM_PHONE 
  189.      */  
  190.     int getActivePhoneType();  
  191.     /** 
  192.      * Returns the CDMA ERI icon index to display 
  193.      */  
  194.     int getCdmaEriIconIndex();  
  195.     /** 
  196.      * Returns the CDMA ERI icon mode, 
  197.      * 0 - ON 
  198.      * 1 - FLASHING 
  199.      */  
  200.     int getCdmaEriIconMode();  
  201.     /** 
  202.      * Returns the CDMA ERI text, 
  203.      */  
  204.     String getCdmaEriText();  
  205.     /** 
  206.      * Returns true if CDMA provisioning needs to run. 
  207.      */  
  208.     boolean getCdmaNeedsProvisioning();  
  209.     /** 
  210.       * Returns the unread count of voicemails 
  211.       */  
  212.     int getVoiceMessageCount();  
  213.     /** 
  214.       * Returns the network type 
  215.       */  
  216.     int getNetworkType();  
  217.       
  218.     /** 
  219.      * Return true if an ICC card is present 
  220.      */  
  221.     boolean hasIccCard();  
  222. }  
 

PhoneCallActivity .java

[java]  view plain copy print ?
  1. package com.geolo.pcp.android;  
  2. import java.lang.reflect.Method;  
  3. import com.android.internal.telephony.ITelephony;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.media.AudioManager;  
  7. import android.os.Bundle;  
  8. import android.telephony.PhoneStateListener;  
  9. import android.telephony.ServiceState;  
  10. import android.telephony.SignalStrength;  
  11. import android.telephony.TelephonyManager;  
  12. import android.util.Log;   
  13. public class PhoneCallActivity extends Activity{  
  14.     private PhoneCallListener mPhoneCallListener;   
  15.     private TelephonyManager mTelephonyManager;  
  16.     //private PhoneStateListener mPhoneStateListener;   
  17.     private AudioManager mAudioManager;   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         mPhoneCallListener = new PhoneCallListener();  
  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.     private void endCall() {  
  32.         // 初始化iTelephony  
  33.         Class<TelephonyManager> c = TelephonyManager.class;  
  34.         Method getITelephonyMethod = 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.             ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null);  
  44.             iTelephony.endCall();  
  45.             Log.v(this.getClass().getName(), "endCall......");  
  46.         } catch (Exception e) {  
  47.             Log.e(this.getClass().getName(), "endCallError", e);  
  48.         }   
  49.     }  
  50.     public class PhoneCallListener extends PhoneStateListener{  
  51.         @Override  
  52.         public void onCallStateChanged(int state, String incomingNumber) {  
  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.             case TelephonyManager.CALL_STATE_IDLE:   
  58.                 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);   
  59.                 break;   
  60.             case TelephonyManager.CALL_STATE_OFFHOOK:   
  61.                 //CALL_STATE_OFFHOOK;   
  62.                 break;   
  63.             case TelephonyManager.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.         public void onDataConnectionStateChanged(int state) {  
  78.             Log.v(this.getClass().getName(), "onDataConnectionStateChanged-state: " + state);  
  79.             super.onDataConnectionStateChanged(state);  
  80.         }  
  81.         @Override  
  82.         public void onDataConnectionStateChanged(int state, int networkType) {  
  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.         public void onServiceStateChanged(ServiceState serviceState) {  
  89.             Log.v(this.getClass().getName(), "onServiceStateChanged-ServiceState: " + serviceState);  
  90.             super.onServiceStateChanged(serviceState);  
  91.         }  
  92.         @Override  
  93.         public void onSignalStrengthChanged(int asu) {  
  94.             Log.v(this.getClass().getName(), "onSignalStrengthChanged-asu: " + asu);  
  95.             super.onSignalStrengthChanged(asu);  
  96.         }  
  97.         @Override  
  98.         public void onSignalStrengthsChanged(SignalStrength signalStrength) {  
  99.             Log.v(this.getClass().getName(), "onSignalStrengthsChanged-signalStrength: " + signalStrength);  
  100.             super.onSignalStrengthsChanged(signalStrength);  
  101.         }  
  102.     }   
  103. }  
 

[java]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.geolo.pcp.android" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".PhoneCallActivity" android:label="@string/app_name">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.     </application>  
  13.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  14.     <uses-permission android:name="android.permission.CALL_PHONE" />  
  15.     <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />  
  16.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
  17.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>  
  18.     <uses-permission android: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值