Android 传入呼叫阻止代码在实际设备中不起作用

Android 传入呼叫阻止代码在实际设备中不起作用,android,Android,我使用以下代码来阻止来电 android中的编程调用 它完全可以在模拟器上运行,但不能在真实设备上运行。请给出一些解决方案 这需要谷歌的许可吗。 而telephonyService.endCall()也不起作用 package com.android.MyCellFamily.DAReceiver; import java.lang.reflect.Method; import com.android.MyCellFamily.DAService.LocationService; import

我使用以下代码来阻止来电
android中的编程调用

它完全可以在模拟器上运行,但不能在真实设备上运行。请给出一些解决方案 这需要谷歌的许可吗。 而telephonyService.endCall()也不起作用

package com.android.MyCellFamily.DAReceiver;
import java.lang.reflect.Method;
import com.android.MyCellFamily.DAService.LocationService;
import com.android.MyCellFamily.DB.ClsDataBaseUtils;
import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class ClsIncomingCallBlocker_DAReceiver extends BroadcastReceiver {
    ClsDataBaseUtils dts=new ClsDataBaseUtils();
    String clsincommingNumber;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        try
        {

            dts.createDatabse("MyCellFamily",context);
            dts.createTable("tbl_Contacts", context); 
            dts.createBlockedStatusTable("tbl_BlockedContacts", context);

            MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
            ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            com.android.internal.telephony.ITelephony telephonyService =(ITelephony)m.invoke(tm);


            Bundle b = intent.getExtras();
            clsincommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.d("clsincommingNumber",clsincommingNumber);
            if(dts.checkPreffContactsFrInCall("tbl_Contacts", clsincommingNumber).equals("true"))
            {
            //Answer Ringing Call
            telephonyService.answerRingingCall();
            dts.close();    
            }   
            else if(dts.checkBlockedStatusFrInCall("tbl_BlockedContacts", clsincommingNumber).equals("true"))
            {
            System.out.println("Incoming blocker"+dts.checkBlockedStatusFrInCall("tbl_BlockedContacts", clsincommingNumber));
            //block incoming call
            telephonyService.endCall();
            dts.close();
            }
            else if(LocationService.getStatusOfDriving().equals("block"))
            {
            System.out.println("Your Status Of Driving is"+LocationService.getStatusOfDriving().toString());
            telephonyService.endCall();
            this.setResultData(null);
            }   
            else
            {
            //Answer Ringing Call
            telephonyService.answerRingingCall();
            dts.close();
            }   

        }
        catch(Exception e)
        {

        }
    }
}

如果Android(或任何其他手机操作系统)现在或将来正式支持程序化呼叫拦截,我会非常惊讶。您尝试这样做的代码基本上是一种黑客行为(它使用反射来获取和调用私有API方法,这是一种巧妙的技巧,但非常脆弱),并且不能保证工作正常

事实上,我完全希望谷歌在未来的Android更新中故意破坏这些代码,如果他们还没有这样做的话。他们所要做的就是将
getITelephony()
重构为
一些听起来不太像的东西,然后添加一个stub
getITelephony()
方法,该方法什么都不做(或者干脆不做,让应用程序处理异常或崩溃)


因此,你正在测试的设备上可能已经堵塞了这个漏洞,即使没有,将来的更新可能会破坏使用这个漏洞的每个应用程序,这只是时间问题。你的开发时间最好花在更新你的应用上,这样它就能尽可能优雅地处理被电话打断的情况

有没有更好的方法来阻止通话?如果你有解决方案,请给出你的评论(或任何其他手机操作系统)。在Symbian上,可以接听然后挂断第三方应用程序(Etel3rdParty)的来电,并通过Etel直接挂断电话(需要特殊权限-例如功能)。