当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 在Android1.5版本之前,实现挂断电话是非常容易的事,只需要调用TelephonyManager的endCall()方法就可以了,但在1.5版本之后,Google工程师为了手机的安全期间,把endCall

 在Android1.5版本之前,实现挂断电话是非常容易的事,只需要调用TelephonyManager的endCall()方法就可以了,但在1.5版本之后,Google工程师为了手机的安全期间,把endCall的方法隐藏掉了。所以实现挂断电话可以通过反射的方法,执行endCall方法。具体实现如下:

TelephonyManager在源码里是这样描述的:Context.getSystemService(Context.TELEPHONY_SERVICE)},我们通过TELEPHONY_SERVICE系统服务调用就可以获取。

?

1
2
3
4
registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    return new TelephonyManager(ctx.getOuterContext());
                }});

而 android.os.ServiceManager 有getService方法

?

1
2
3
4
5
6
7
8
9
/**
     * Returns a reference to a service with the given name.
     *
     * @param name the name of the service to get
     * @return a reference to the service, or <code>null</code> if the service doesn't exist
     */
    public static IBinder getService(String name) {
        return null;
    }

android.telephony.TelephonyManager 类是代理ITelephony接口的,可以看到里面的endCall是被隐藏掉的。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Interface used to interact with the phone.  Mostly this is used by the
 * TelephonyManager class.  A few places are still using this directly.
 * Please clean them up if possible and use TelephonyManager insteadl.
 *
 * {@hide}
 */
interface ITelephony {
 
    /**
     * Dial a number. This doesn't place the call. It displays
     * the Dialer screen.
     * @param number the number to be dialed. If null, this
     * would display the Dialer screen with no number pre-filled.
     */
 
/**
     * End call or go to the Home screen
     *
     * @return whether it hung up
     */
    boolean endCall();

我们知道利用绑定服务可以调用里面的方法,会返回一个IBinder对象,利用IBinder可以调用服务里的方法。

TelephonyManager实际上就是系统电话服务的代理对象,通过aidl获取IBinder,然后进一步的进行封装。代理有限的方法。

TelephonyManager ------------------IBinder-------------------------系统服务

代理对象 aidl

所以实现需要一下步奏:

1 、 反射加载ServiceManager类。

2、 获取IBinder对象执行getService方法。

3、 在项目中新建com.android.internal.telephony包(包名不能变),然后拷贝ITelephony.aidl。 在新建android.telephony包,拷贝NeighboringCellInfo.aidl 的进程通信接口两个文件。

4、调用endCall方法。

实现方法如下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void endCall() {
            // IBinder iBinder = ServiceManager.getService(TELEPHONY_SERVICE);
            // ServiceManager 是被系统隐藏掉了 所以只能用反射的方法获取
            try {
                // 加载ServiceManager的字节码
                Class<!--?--> clazz = CallSMSSafeService.class.getClassLoader()
                        .loadClass("android.os.ServiceManager");
                Method method = clazz.getDeclaredMethod("getService",
                        String.class);
                IBinder iBinder = (IBinder) method.invoke(null,
                        TELEPHONY_SERVICE);
                ITelephony.Stub.asInterface(iBinder).endCall();
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(TAG, "拦截电话异常");
            }
 
        }
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭