再此加上文件目录
MainActivity代码
package com.example.call;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private Method method;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过反射机制得到类
try {
//得到类
Class clazz = Class.forName("android.os.ServiceManager");
//得到方法
method = clazz.getMethod("getService", String.class);
//调用方法(静态方法不用传对象)
IBinder iBinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE);
//得到远程服务接口
ITelephony telePhone = ITelephony.Stub.asInterface(iBinder);
//打电话功能
telePhone.call("15686207622");
} catch (ClassNotFoundException e) {
//没有找到这个类
Log.i("123", "没有找到这个类");
e.printStackTrace();
} catch (NoSuchMethodException e) {
// 没有找到这个方法
Log.i("123", "没有找到这个方法");
e.printStackTrace();
} catch (IllegalAccessException e) {
// 方法调用失败
Log.i("123", "方法调用失败");
e.printStackTrace();
} catch (IllegalArgumentException e) {
// 方法调用失败
Log.i("123", "方法调用失败");
e.printStackTrace();
} catch (InvocationTargetException e) {
// 方法调用失败
Log.i("123", "方法调用失败");
e.printStackTrace();
} catch (RemoteException e) {
// 拨打电话失败
Log.i("123", "拨打电话失败");
e.printStackTrace();
}
}
}