While blocking incoming call in android, it ring briefly for fractions of second, how to overcome this issue

275 Views Asked by At

I have a broadcast receiver, add before blocking the call matching the incoming number with the local database also i have added below permissions in my manifest and also register this broadcast receiver:

@Override
public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();
  if(bundle != null){
      try {
String state = bundle.getString(TelephonyManager.EXTRA_STATE);           
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){      
               if(!isChecking){
                  isChecking = true;
                  String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
HashMap<String, String> minelist =  getAllBlockedContacts(context);
boolean isBlockedNumber = false;
                 if(minelist != null){
if(minelist.containsKey(incomingnumber))
                        isBlockedNumber = true;
                    else {
Iterator<String> myVeryOwnIterator = minelist.keySet().iterator();
                          while(myVeryOwnIterator.hasNext()) {
                              String number=(String)myVeryOwnIterator.next();
                              String countrycode=(String)minelist.get(number);
                              String finalNumber = countrycode + number;
if(finalNumber.equals(incomingnumber)){
                                    isBlockedNumber = true;
                                    break;
}
                              else if (("+"+finalNumber).equals("+"+incomingnumber)){
                                    isBlockedNumber = true;
                                    break;
                              }else if(("0"+number).equals(incomingnumber)){
                                    isBlockedNumber = true;
                                    break;                                    
                              }
}
                    }
                 } 
if(isBlockedNumber){                                                
                    endCall(context);

                }
                else
                  isChecking = false;
              }
          }
else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
              isChecking = false;
              Log.v(TAG, "Call State OffHook");
          }
          else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
              isChecking = false;
              Log.v(TAG, "Call State Idle");
                }                
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

private void endCall(Context context){

    try {
        // Get the boring old TelephonyManager.
        TelephonyManager telephonyManager =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        //get the class telephone
        Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());

        // Get the getITelephony() method     
        Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

        // Ignore that the method is supposed to be private                      
        methodGetITelephony.setAccessible(true);

        // Invoke getITelephony() to get the ITelephony interface
        Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

        // Get the endCall method from ITelephony
        Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());

        // Get the method end call
        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

        // Invoke endCall()
        methodEndCall.invoke(telephonyInterface);

    } catch (Exception e) {
        e.printStackTrace();

} 
}
/**
 * Method used to get the list of all blocked list numbers
 * @return
 */
public HashMap<String, String> getAllBlockedContacts(Context context) {
    //ba.textfile("In getAllBlockedContacts");
    ba.saveMessage(context,"In getAllBlockedContacts" );

    HashMap<String, String> contactlist = null;
    DBHelper dbhelper = null;
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        String selectQuery = "SELECT * FROM blocktable";
        dbhelper = new DBHelper(context);
        db = dbhelper.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            contactlist = new HashMap<String, String>();
            //ba.textfile("Cursor Key Length" +cursor.getCount() );
            ba.saveMessage(context,"Cursor Key Length" +cursor.getCount()  );

            while(!cursor.isAfterLast()){
                String countryCode = "+"+cursor.getString(cursor.getColumnIndex("countrycode"));
                String blockedNo =   cursor.getString(cursor.getColumnIndex("blockedno"));
                //Log.e(TAG, "Blocked Numbers "+countryCode+blockedNo);
                contactlist.put(blockedNo, countryCode);
                cursor.moveToNext();


            }
            db.close();
        }
        //db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally{
        if(cursor != null){
            cursor.close();
            cursor = null;
        }
        if(db != null){
            db.close();
            db = null;
        }
        if(dbhelper != null){
            dbhelper.close();
            dbhelper = null;
        }
    }
    return contactlist;
}

}

0

There are 0 best solutions below