0

I have use the following code for incoming call blocking How to reject incoming call programatically in android?
call-programatically-in-android

It completely works in emulator but not on real device please give mw some solution Is that any permission required from google. and telephonyService.endCall() also not working.

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)
        {

        }
    }
}
Community
  • 1
  • 1
bindal
  • 1
  • 2

1 Answers1

1

I would be very surprised if programmatic call blocking is or ever will be officially supported by Android (or any other phone OS). The code you are using to try to do so is basically a hack (it uses reflection to grab and invoke a private API method, which is a neat trick but brittle as anything), and not guaranteed to work.

In fact, I would fully expect Google to deliberately break such code in some future Android update, if they haven't already. All they would have to do is refactor getITelephony() to somethingThatSoundsLessLikeAnObviousExploitPoint() and then add a stub getITelephony() method that does nothing (or just leave it out and let apps handle the Exception or crash).

So it may be that this hole has already been plugged on the device you are testing with, and even if it hasn't it's probably only a matter of time before some future update breaks every app that is using this hack. Your development time might be better spent updating your app so that it handles being interrupted by a phone call as gracefully as possible.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • IS there Any better way to block a call if you have a solution please give me – bindal Mar 26 '11 at 12:18
  • Regarding your remark "(or any other phone OS)". On Symbian it's possible to answer and then hang up an incoming phone call for 3rd party applications (Etel3rdParty) and via Etel hang up the call directly (requires special permissions - e.g. capabilities). – tidbeck Oct 07 '11 at 09:45