原文:http://topic.csdn.net/u/20091226/00/09e2979b-7c8c-4991-856d-586a278875c0.html
出处: http://blog.csdn.net/li_guotao/article/details/6347517
1. 在src下新建一个包:com.android.internal.telephony(和ITelephony.aidl 中 package 声明的包名一样);
2. 然后从系统源码中把ITelephony.aidl文件拷贝到 com.android.internal.telephony包下。(也可以先新建一个ITelephony.aidl, 然后再把内容拷贝进去(ITelephony.aidl文件内容可以在http://www.netmite.com/android/mydroid/1.5/frameworks/base/telephony/java/com/android/internal/telephony/ITelephony.aidl找到,这个方法适合没有SDK源码文件的开发者));
3. 在src下新建一个包:android.telephony, 然后新建一个NeighboringCellInfo.aidl,其内容为:
package android.telephony;
parcelable NeighboringCellInfo;
4.从私有方法中获取ITelephony: 代码如下。
tManager = (TelephonyManager)
this.getSystemService(Context.TELEPHONY_SERVICE);
//初始化iTelephony
Class <TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[])null);
getITelephonyMethod.setAccessible(true);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
iTelephony = (ITelephony) getITelephonyMethod.invoke(tManager, (Object[])null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PhoneCallActivity .java
package com.geolo.pcp.android;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneCallActivity extends Activity{
private PhoneCallListener mPhoneCallListener;
private TelephonyManager mTelephonyManager;
//private PhoneStateListener mPhoneStateListener;
private AudioManager mAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPhoneCallListener = new PhoneCallListener();
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()结束通话。
*/
private void endCall() {
// 初始化iTelephony
Class<TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
// 获取所有public/private/protected/默认
// 方法的函数,如果只需要获取public方法,则可以调用getMethod.
getITelephonyMethod = c.getDeclaredMethod("getITelephony",(Class[]) null);
// 将要执行的方法对象设置是否进行访问检查,也就是说对于public/private/protected/默认
// 我们是否能够访问。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false
// 则指示反射的对象应该实施 Java 语言访问检查。
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(mTelephonyManager, (Object[]) null);
iTelephony.endCall();
Log.v(this.getClass().getName(), "endCall......");
} catch (Exception e) {
Log.e(this.getClass().getName(), "endCallError", e);
}
}
public class PhoneCallListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.v(this.getClass().getName(), "onCallStateChanged-state: " + state);
Log.v(this.getClass().getName(), "onCallStateChanged-incomingNumber: " + incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
break;
case TelephonyManager.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
public void onDataConnectionStateChanged(int state) {
Log.v(this.getClass().getName(), "onDataConnectionStateChanged-state: " + state);
super.onDataConnectionStateChanged(state);
}
@Override
public void onDataConnectionStateChanged(int state, int networkType) {
Log.v(this.getClass().getName(), "onDataConnectionStateChanged-state: " + state);
Log.v(this.getClass().getName(), "onDataConnectionStateChanged-networkType: " + networkType);
super.onDataConnectionStateChanged(state, networkType);
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {
Log.v(this.getClass().getName(), "onServiceStateChanged-ServiceState: " + serviceState);
super.onServiceStateChanged(serviceState);
}
@Override
public void onSignalStrengthChanged(int asu) {
Log.v(this.getClass().getName(), "onSignalStrengthChanged-asu: " + asu);
super.onSignalStrengthChanged(asu);
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
Log.v(this.getClass().getName(), "onSignalStrengthsChanged-signalStrength: " + signalStrength);
super.onSignalStrengthsChanged(signalStrength);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geolo.pcp.android" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PhoneCallActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>