-1

want to display overlay (like truecaller app) while incoming call. I checked Pop up window over Android native incoming call screen like true caller Android app this link and tried to execute but it displays values in separate page..

Updates: I solved this using WindowManager but now problem is i cant remove the view i tried as follows please help me to solve this problem

  public void onReceive(Context context, Intent intent) {
    Log.i("", "Intent " + intent.toString());
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { 
        String mVal;
        try {
            mVal = new GetCallContacts(context).execute().get();

            wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            WindowManager.LayoutParams params;
            params = new WindowManager.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE ,
                    PixelFormat.TRANSPARENT); 

            params.gravity = Gravity.TOP;
            LayoutInflater mInflater = LayoutInflater.from(context);
            mView = mInflater.inflate(R.layout.activity_incoming_call, null);
            TextView tv = (TextView) mView.findViewById(R.id.textView1);
            tv.setText("BJP Contacts: "+mVal);   

                wm.addView(mView, params);
        } catch (Exception e) {
            Log.e(state, e.toString())  ;
        }  
   } else if (intent.getAction().equals(BOOT_COMPLETED)) {
        context.startService(intent);

   } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)
          || state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

      try {


        ((WindowManager)  context.getSystemService(Context.WINDOW_SERVICE)).removeView(mView);//wm.removeView(mView);// it does not remove view
      } catch (Exception e) {
      }
  }  

}

Community
  • 1
  • 1
Namitha
  • 11
  • 1
  • 2

1 Answers1

0

Try this:

wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
            params1 = new WindowManager.LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    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);

            params1.height = 280;
            params1.width = LayoutParams.MATCH_PARENT;
            params1.gravity = Gravity.TOP;
            params1.format = PixelFormat.TRANSLUCENT;

            ly1 = new LinearLayout(mcontext.getApplicationContext());
            ly1.setOrientation(LinearLayout.HORIZONTAL);
            ly1.setBackgroundColor(Color.WHITE);
            View hiddenInfo = inflater.inflate(R.layout.call_screen_overlay, ly1,
false);
            tv1 = (TextView) hiddenInfo.findViewById(R.id.number);
             tv1.setText(incomingNumber);
             tv2 = (TextView) hiddenInfo.findViewById(R.id.ctype);
             tv2.setText("Incoming...");
            tv2.setText("Incoming...");
            ly1.addView(hiddenInfo);
            wm.addView(ly1, params1);

And in your EXTRA_STATE_IDLE or EXTRA_STATE_OFFHOOK remove the view using this condition:

if(ly1!=null)
            {
                Log.i("STage", "Remove View");
                wm.removeView(ly1);
                ly1 = null;
            }

Hope it helps.

Srijith
  • 1,695
  • 17
  • 29
  • This creates multiple overlays, and it cannot be closed by this function. Weird. Hello from 5 years later. – Fractal Jun 18 '19 at 14:20