Developer
Developer

Reputation: 385

Call block in android

I am facing problem with android call block option. I am making an app in which i want to provide the user to block the unwanted call . I have googled it but find a solution which is not working in my case. I want to add that i am trying this with my emulator - emulator call service. And number of one of my emulator is 15555215556 and another is 15555215554. Plese help me with code . I am posting here the error that i have faced.

02-22 23:44:05.769: E/BroadcastReceiver(377): BroadcastReceiver trying to return result during a non-ordered broadcast
02-22 23:44:05.769: E/BroadcastReceiver(377): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:451)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.content.BroadcastReceiver.setResultData(BroadcastReceiver.java:266)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at com.example.CallReceiver.onReceive(CallReceiver.java:33)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.os.Looper.loop(Looper.java:123)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at android.app.ActivityThread.main(ActivityThread.java:3683)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at java.lang.reflect.Method.invokeNative(Native Method)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at java.lang.reflect.Method.invoke(Method.java:507)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-22 23:44:05.769: E/BroadcastReceiver(377):   at dalvik.system.NativeStart.main(Native Method)

and here is my code:

package com.example;

import java.lang.reflect.Method;
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.TelephonyManager;
import android.util.Log;

public class CallReceiver extends BroadcastReceiver{

      private String incomingnumber;

      @Override
      public void onReceive(Context context, Intent intent) {
            String s[]={"15555215554","15555215556"};
            Bundle b = intent.getExtras();
            incomingnumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            //
            try {

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

                  for (int i = 0; i < s.length; i++) {
                        if(s[i].equals(incomingnumber)){
                            //  telephonyService.endCall();

                              Log.e("incoming:",incomingnumber);

                        }
                  }


            }
            catch (Exception e) {
                  // TODO: handle exception
                e.printStackTrace();
                 Log.e("incoming:","hide");
            }
      }
}

itelephony interface:

package com.android.internal.telephony;

public interface ITelephony
{

      boolean endCall();
      void answerRingingCall();

}

I have provided only one uses permission and that is : <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 0

Views: 2067

Answers (2)

Ramesh Kanuganti
Ramesh Kanuganti

Reputation: 285

    in on Receive 

incomingnumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

    for(String number : s){
if(number.equals(incomingnumber))
//call the method
disconnectPhoneItelephony();
}



        write a method like 

        public void disconnectPhoneItelephony() {      
                ITelephony telephonyService;
                TelephonyManager telephony = (TelephonyManager)
                        context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Class c = Class.forName(telephony.getClass().getName());
                    Method m = c.getDeclaredMethod("getITelephony");
                    m.setAccessible(true);
                    SessionManager.getInstance(context).setBlockStatusAllow("BLOCKED");
                    telephonyService = (ITelephony) m.invoke(telephony);
                    //telephonyService.silenceRinger();
                    telephonyService.endCall();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

// Add permission in Manifest Register Receiver and give nessasary permissions

<receiver android:name="..CallReceiver">
            <intent-filter>                
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>

Upvotes: 0

user2100401
user2100401

Reputation:

I think you should add this:

 <uses-permission android:name="android.permission.CALL_PHONE" />

Upvotes: 2

Related Questions