当前位置:操作系统 > 安卓/Android >>

android 监听电话去电接通瞬间[非来电]

首先感叹一下android强大,它可以把同一个apk里面得不同Actvity分别运行在不同的进程中,比如我想让自己的应用中Activity运行在Phone进程中,那么我需要做三个事情。

本文包含三个知识点:

1.监听去电接通     2.apk获取root权限 3. 运行时将apk push 到system/app

(1)

 

[java]
<activity   
ndroid:process="com.android.phone" 
            android:label="@string/app_name"   
            android:name=".AutoCallActivity" >   
               
        </activity>   

    <activity 
   android:process="com.android.phone"
                android:label="@string/app_name" 
                android:name=".AutoCallActivity" > 
                 
            </activity>  (2)


[java]
<manifest xmlns:android="http://schemas.android.com/apk/res/android"   
    package="com.spreadst.drag"   
    coreApp="true"   
oid:sharedUserId="android.uid.system" 
    android:versionCode="1"   

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.spreadst.drag" 
        coreApp="true" 
android:sharedUserId="android.uid.system"
        android:versionCode="1" 
(3) [java]
   LOCAL_PACKAGE_NAME := autocall   
    #LOCAL_CERTIFICATE := shared   
LOCAL_CERTIFICATE := platform  

    LOCAL_PACKAGE_NAME := autocall 
    #LOCAL_CERTIFICATE := shared 
LOCAL_CERTIFICATE := platform 为什么会有这个需求呢?TelephonyManager 中状态 只有  IDLE 空闲状态, 来电接通状态 ,拨打状态,没有去电接通状态,参考phone中得实现如下:
Phone  phone = PhoneFactory.getDefaultPhone();
这个调用是不能运行在phone以外的进程中的,不信你可以试一下,请记住一个进程的主线程的looper是唯一的

原因如下:

[java]
if (sLooper != Looper.myLooper()) { 
            throw new RuntimeException( 
                "PhoneFactory.getDefaultPhone must be called from Looper thread"); 
        } 

if (sLooper != Looper.myLooper()) {
            throw new RuntimeException(
                "PhoneFactory.getDefaultPhone must be called from Looper thread");
        }去电接通若干秒之后挂断代码如下,由于使用了hide class,所以必须在android源代码底下编译,当然你也可以通过反射、AIDL、class.jar等方式解决这个问题。


[java]
CallManager mCM = CallManager.getInstance();   
   Phone  phone = PhoneFactory.getDefaultPhone();   
   
mCM.registerPhone(phone);   
       mCM.registerForPreciseCallStateChanged(mHandler, PHONE_STATE_CHANGED, null);  

CallManager mCM = CallManager.getInstance(); 
   Phone  phone = PhoneFactory.getDefaultPhone(); 
 
mCM.registerPhone(phone); 
       mCM.registerForPreciseCallStateChanged(mHandler, PHONE_STATE_CHANGED, null);
[java]
private Handler mHandler=new Handler(){   
   
        public void handleMessage(android.os.Message msg) {   
            switch (msg.what) {   
            case PHONE_STATE_CHANGED:   
                updatePhoneSateChange();   
                break;   
            default:   
                break;   
            }   
        };   
           
    };   

    private Handler mHandler=new Handler(){ 
     
            public void handleMessage(android.os.Message msg) { 
                switch (msg.what) { 
                case PHONE_STATE_CHANGED: 
                    updatePhoneSateChange(); 
                    break; 
                default: 
                    break; 
                } 
            }; 
             
        }; 
[java]
private void updatePhoneSateChange(){   
        Call fgCall = mCM.getActiveFgCall();   
        if (mCM.hasActiveRingingCall()) {   
                fgCall = mCM.getFirstActiveRingingCall();   
          }   
   
        final Call.State state = fgCall.getState();   
        switch (state) {   
         

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,