Android提高第十四篇之探秘TelephonyManager

來源:互聯網
上載者:User

上次介紹了如何使用JAVA的反射機制來調用藍芽的隱藏API,這次繼續練習JAVA的反射機制,探秘TelephonyManager在Framework裡包含卻在SDK隱藏的幾項功能。先來看看本文程式啟動並執行:

 

本文程式示範了以下功能:

1.所有來電自動接聽;

2.所有來電自動掛斷;

3.開啟/關閉Radio;

4.開啟/關閉資料連線(WAP or NET的串連)。

調用TelephonyManager的隱藏API是先參考Framework的aseelephonyjavacomandroidinternalelephonyITelephony.aidl,然後自己實現一個ITelephony.aidl,最後在TelephonyManager中通過反射機制執行個體化自訂的ITelephony,執行個體化之後就可以調用ITelephony裡面的函數了。

本文程式需要在AndroidManifest.xml添加以下兩行代碼,以獲得許可權:

view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

main.xml源碼如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <RadioGroup android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/rGrpSelect"> 
        <RadioButton android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/rbtnAutoAccept" 
            android:text="所有來電自動接聽"></RadioButton> 
        <RadioButton android:layout_height="wrap_content" 
            android:layout_width="fill_parent" android:id="@+id/rbtnAutoReject" 
            android:text="所有來電自動掛斷"></RadioButton> 
    </RadioGroup> 
    <ToggleButton android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/tbtnRadioSwitch" 
        android:textOn="Radio已經啟動" android:textOff="Radio已經關閉" 
        android:textSize="24dip" android:textStyle="normal"></ToggleButton> 
    <ToggleButton android:layout_height="wrap_content" 
        android:layout_width="fill_parent" android:id="@+id/tbtnDataConn" 
        android:textSize="24dip" android:textStyle="normal" android:textOn="允許資料連線" 
        android:textOff="禁止資料連線"></ToggleButton> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <RadioGroup android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/rGrpSelect">
  <RadioButton android:layout_height="wrap_content"
   android:layout_width="fill_parent" android:id="@+id/rbtnAutoAccept"
   android:text="所有來電自動接聽"></RadioButton>
  <RadioButton android:layout_height="wrap_content"
   android:layout_width="fill_parent" android:id="@+id/rbtnAutoReject"
   android:text="所有來電自動掛斷"></RadioButton>
 </RadioGroup>
 <ToggleButton android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/tbtnRadioSwitch"
  android:textOn="Radio已經啟動" android:textOff="Radio已經關閉"
  android:textSize="24dip" android:textStyle="normal"></ToggleButton>
 <ToggleButton android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/tbtnDataConn"
  android:textSize="24dip" android:textStyle="normal" android:textOn="允許資料連線"
  android:textOff="禁止資料連線"></ToggleButton>
</LinearLayout>
 

PhoneUtils.java是手機功能類,從TelephonyManager中執行個體化ITelephony並返回,源碼如下:

view plaincopy to clipboardprint?
package com.testTelephony;  
 
import java.lang.reflect.Field;  
import java.lang.reflect.Method;  
import com.android.internal.telephony.ITelephony;  
import android.telephony.TelephonyManager;  
import android.util.Log;  
 
public class PhoneUtils {  
    /** 
     * 從TelephonyManager中執行個體化ITelephony,並返回 
     */ 
    static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception {  
        Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");  
        getITelephonyMethod.setAccessible(true);//私人化函數也能使用  
        return (ITelephony)getITelephonyMethod.invoke(telMgr);  
    }  
      
    static public void printAllInform(Class clsShow) {    
        try {    
            // 取得所有方法    
            Method[] hideMethod = clsShow.getDeclaredMethods();    
            int i = 0;    
            for (; i < hideMethod.length; i++) {    
                Log.e("method name", hideMethod[i].getName());    
            }    
            // 取得所有常量    
            Field[] allFields = clsShow.getFields();    
            for (i = 0; i < allFields.length; i++) {    
                Log.e

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.