Android提高之TelephonyManager功能探秘
前面文章介紹了如何使用JAVA的反射機(jī)制來調(diào)用藍(lán)牙的隱藏API,本文繼續(xù)來練習(xí)JAVA的反射機(jī)制,探秘TelephonyManager在Framework里包含卻在SDK隱藏的幾項(xiàng)功能。
先來看一下本文程序運(yùn)行的效果圖,如下所示:

本文程序演示了以下功能:
1.所有來電自動接聽;
2.所有來電自動掛斷;
3.開啟/關(guān)閉Radio;
4.開啟/關(guān)閉數(shù)據(jù)連接(WAP or NET的連接)。
調(diào)用TelephonyManager的隱藏API是先參考Framework的/base/telephony/java/com/android/internal/telephony/ITelephony.aidl,然后自己實(shí)現(xiàn)一個(gè)ITelephony.aidl,最后在TelephonyManager中通過反射機(jī)制實(shí)例化自定義的ITelephony,實(shí)例化之后就可以調(diào)用ITelephony里面的函數(shù)了。
本文程序需要在AndroidManifest.xml添加以下兩行代碼,以獲得權(quán)限:
<uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
main.xml源碼如下:
<?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已經(jīng)啟動" android:textOff="Radio已經(jīng)關(guān)閉" 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="允許數(shù)據(jù)連接" android:textOff="禁止數(shù)據(jù)連接"></ToggleButton> </LinearLayout>
PhoneUtils.java是手機(jī)功能類,從TelephonyManager中實(shí)例化ITelephony并返回,源碼如下:
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中實(shí)例化ITelephony,并返回
*/
static public ITelephony getITelephony(TelephonyManager telMgr) throws Exception {
Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函數(shù)也能使用
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("Field name", allFields[i].getName());
}
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
testTelephony.java是主類,使用PhoneStateListener監(jiān)聽通話狀態(tài),以及實(shí)現(xiàn)上述4種電話控制功能,源碼如下:
package com.testTelephony;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.ToggleButton;
public class testTelephony extends Activity {
/** Called when the activity is first created. */
RadioGroup rg;//來電操作單選框
ToggleButton tbtnRadioSwitch;//Radio開關(guān)
ToggleButton tbtnDataConn;//數(shù)據(jù)連接的開關(guān)
TelephonyManager telMgr;
CallStateListener stateListner;
int checkedId=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
telMgr= (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telMgr.listen(new CallStateListener(), CallStateListener.LISTEN_CALL_STATE);
PhoneUtils.printAllInform(TelephonyManager.class);
rg = (RadioGroup)findViewById(R.id.rGrpSelect);
rg.setOnCheckedChangeListener(new CheckEvent());
tbtnRadioSwitch=(ToggleButton)this.findViewById(R.id.tbtnRadioSwitch);
tbtnRadioSwitch.setOnClickListener(new ClickEvent());
try {
tbtnRadioSwitch.setChecked(PhoneUtils.getITelephony(telMgr).isRadioOn());
} catch (Exception e) {
Log.e("error",e.getMessage());
}
tbtnDataConn=(ToggleButton)this.findViewById(R.id.tbtnDataConn);
tbtnDataConn.setOnClickListener(new ClickEvent());
try {
tbtnDataConn.setChecked(PhoneUtils.getITelephony(telMgr).isDataConnectivityPossible());
} catch (Exception e) {
Log.e("error",e.getMessage());
}
}
/**
* 來電時(shí)的操作
* @author GV
*
*/
public class CheckEvent implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
testTelephony.this.checkedId=checkedId;
}
}
/**
* Radio和數(shù)據(jù)連接的開關(guān)
* @author GV
*
*/
public class ClickEvent implements View.OnClickListener{
@Override
public void onClick(View v) {
if (v == tbtnRadioSwitch) {
try {
PhoneUtils.getITelephony(telMgr).setRadio(tbtnRadioSwitch.isChecked());
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
else if(v==tbtnDataConn){
try {
if(tbtnDataConn.isChecked())
PhoneUtils.getITelephony(telMgr).enableDataConnectivity();
else if(!tbtnDataConn.isChecked())
PhoneUtils.getITelephony(telMgr).disableDataConnectivity();
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
}
}
/**
* 監(jiān)視電話狀態(tài)
* @author GV
*
*/
public class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(state==TelephonyManager.CALL_STATE_IDLE)//掛斷
{
Log.e("IDLE",incomingNumber);
}
else if(state==TelephonyManager.CALL_STATE_OFFHOOK)//接聽
{
Log.e("OFFHOOK",incomingNumber);
}
else if(state==TelephonyManager.CALL_STATE_RINGING)//來電
{
if(testTelephony.this.checkedId==R.id.rbtnAutoAccept)
{
try {
//需要<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
PhoneUtils.getITelephony(telMgr).silenceRinger();//靜鈴
PhoneUtils.getITelephony(telMgr).answerRingingCall();//自動接聽
} catch (Exception e) {
Log.e("error",e.getMessage());
}
}
else if(testTelephony.this.checkedId==R.id.rbtnAutoReject)
{
try {
PhoneUtils.getITelephony(telMgr).endCall();//掛斷
PhoneUtils.getITelephony(telMgr).cancelMissedCallsNotification();//取消未接顯示
} catch (Exception e) {
Log.e("error",e.getMessage());
}
}
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
感興趣的讀者可以測試一下本文實(shí)例代碼,希望能夠?qū)Υ蠹业腁ndroid項(xiàng)目開發(fā)有所幫助。
相關(guān)文章
Android 通過onDraw實(shí)現(xiàn)在View中繪圖操作的示例
以下是對Android通過onDraw實(shí)現(xiàn)在View中繪圖操作的示例代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
Android簡單實(shí)現(xiàn)屏幕下方Tab菜單的方法
這篇文章主要介紹了Android簡單實(shí)現(xiàn)屏幕下方Tab菜單的方法,簡單分析了Android實(shí)現(xiàn)tab菜單所涉及的界面布局及功能相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android 中 viewpager 滑動指示器的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了android 中 viewpager 滑動指示器,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Android中自定義加載樣式圖片的具體實(shí)現(xiàn)
想實(shí)現(xiàn)下面這張圖中的自定義加載樣式,其實(shí)很簡單,首先我們需要的布局組件有ProcessBar和TextView,下面是布局文件的代碼2014-04-04
Android實(shí)現(xiàn)基于滑動的SQLite數(shù)據(jù)分頁加載技術(shù)(附demo源碼下載)
這篇文章主要介紹了Android實(shí)現(xiàn)基于滑動的SQLite數(shù)據(jù)分頁加載技術(shù),涉及Android針對SQLite數(shù)據(jù)的讀取及查詢結(jié)果的分頁顯示功能相關(guān)實(shí)現(xiàn)技巧,末尾還附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-07-07
Android中使用開源框架Citypickerview實(shí)現(xiàn)省市區(qū)三級聯(lián)動選擇
這篇文章主要介紹了Android中使用開源框架Citypickerview實(shí)現(xiàn)省市區(qū)三級聯(lián)動選擇效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android中獲取sha1證書指紋數(shù)據(jù)的方法
大家都知道在Android開發(fā)中,經(jīng)常要獲取sha1證書指紋,所以這篇文章主要介紹在Android中如何使用命令獲取sha1證書指紋數(shù)據(jù)的方法,有需要的可以參考借鑒。2016-09-09
Android ExpandableListView單選以及多選實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android ExpandableListView單選以及多選的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識,在Android應(yīng)用開發(fā)過程中會遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法2016-08-08

