0

We are trying to implement popup like true-caller in our application. It was successfully working, when we get any call its showing pop-up. But when call disconnected, Then pop-up is not removing. We tried all, But not working. Please help.

Here is our Broadcast class:-

PhonecallReceiver.java

public  class PhonecallReceiver extends BroadcastReceiver
{
    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;
    View view;
    @Override
    public void onReceive(Context context, Intent intent)
    {
        try
        {
            if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
            {
                savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
            }
            else
            {
                String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
                String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                int state = 0;
                if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE))
                {
                    state = TelephonyManager.CALL_STATE_IDLE;
                }
                else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
                {
                    state = TelephonyManager.CALL_STATE_OFFHOOK;
                }
                else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING))
                {
                    state = TelephonyManager.CALL_STATE_RINGING;
                }

                onCallStateChanged(context, state, number);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //Derived classes should override these to respond to specific events of interest
    protected void onIncomingCallStarted(Context context, String number, Date start){


//                onCallStateChanged(context, state, number);

    }
    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){


    }

    public void onCallStateChanged(Context context, int state, String number)
    {
        if(lastState == state)
        {
            //No change, debounce extras
            return;
        }
        switch (state)
        {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = number;
                onIncomingCallStarted(context, number, callStartTime);
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                if (isIncoming)
                {
                    onIncomingCallEnded(context,savedNumber,callStartTime,new Date());
                }

            case TelephonyManager.CALL_STATE_IDLE:
                if(isIncoming)
                {
                    onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
                }
        }
        lastState = state;
    }
}

CallReciever.java

public class CallReceiver extends PhonecallReceiver
{
    Context context;
    View view ;
    LinearLayout ly;
    WindowManager wm;
    @Override
    protected void onIncomingCallStarted( Context ctx, final String number, Date start)
    {
        Toast.makeText(ctx,"Kushal Incoming Call"+ number,Toast.LENGTH_LONG).show();

        context =   ctx;
                 ly = new LinearLayout(context);
        LayoutInflater mInflater = LayoutInflater.from(context);
        view = mInflater.inflate(R.layout.dialog,ly ,true);
                 wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

                WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
                        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSPARENT);

                params.height =500;
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.format = PixelFormat.TRANSLUCENT;

                params.gravity = Gravity.TOP;



                // view = ly.inflate(R.layout.dialog_layout,null);
                TextView textview = (TextView) view.findViewById(R.id.tv_client);
                textview.setText("Someone is calling: " + number);
                textview.setBackgroundColor(Color.red(4));
              //  ly.setBackgroundColor(Color.RED);
                //ly.setOrientation(LinearLayout.VERTICAL);



                wm.addView(view, params);
        view.findViewById(R.id.dialog_ok).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // do stuff
                wm.removeView(view);

            }
        });
    }
    public void releaseLandScape(){
//        ((WindowManager) context.getApplicationContext().getSystemService(Service.WINDOW_SERVICE)).removeView(view);
        wm.removeView(view); //error on this line
        // This doesn't work as well
        //wm.removeViewImmediate(orientationChanger);
    }


    @Override
    protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
    {
        try {
            Toast.makeText(ctx, "Bye Bye" + number, Toast.LENGTH_LONG).show();
            WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
            releaseLandScape();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

ERROR:-

java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.WindowManager.removeView(android.view.View)' on a null object reference

If you chceck carefully, Then you can see in "CallReciever.java", On incoming call, I initilize view, But when call is ending then view is null. Thats the problem.

pankaj agarwal
  • 171
  • 2
  • 15
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – earthw0rmjim Dec 07 '16 at 17:39
  • No, If you chceck carefully, Then you can see in "CallReciever.java", On incoming call, I initilize view, But when call is ending then view is null. Thats the problem. – pankaj agarwal Dec 07 '16 at 17:42
  • 1
    Assuming your `CallReceiver` is registered in the manifest, the root of your problems is that each time `CallReceiver` runs, it's a new, different instance. The `wm` you initialized when the call started isn't there anymore when the call ends. Same for `view`. You'll need use a `Service` to maintain a reference to the `View` you're adding to `WindowManager`. (Btw, your current specific Exception is because you're declaring a `WindowManager wm` local to `onIncomingCallEnded()`, and the field `wm` never gets initialized before you call `removeView()` on it.) – Mike M. Dec 07 '16 at 18:28
  • Thanks. Using service is great idea. – pankaj agarwal Dec 07 '16 at 21:04

0 Answers0