Can't remove overlay view
Asked Answered
B

2

6

Update

I was able to fix this, The problem was dialogs and related could be initiated from broadcast receiver but not recommended, as the activity running ends before the view.

Trying to implement overlay like fb messenger, truecaller etc.

public class IncomingCall extends BroadcastReceiver
{
private Context pcontext;
private static final String TAG = "CustomBroadcastReceiver";
 TelephonyManager telephony;
 CustomPhoneStateListener customPhoneListener ;
@Override
public void onReceive(Context context, Intent intent) 
{
    pcontext = context;
    Bundle extras = intent.getExtras();
    if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);
        Log.w("DEBUG", state);

            telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            customPhoneListener = new   CustomPhoneStateListener();
            telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
            Bundle bundle = intent.getExtras();
            String phoneNr= bundle.getString("incoming_number");


    }


}
public class CustomPhoneStateListener extends PhoneStateListener
{
    private static final String TAG = "CustomPhoneStateListener";
    Handler handler=new Handler();
    @Override
    public void onCallStateChanged(int state, String incomingNumber) 
    {

        WindowManager wm = (WindowManager) pcontext.getSystemService(Context.WINDOW_SERVICE);

        WindowManager.LayoutParams 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_FOCUSABLE,
            PixelFormat.TRANSPARENT);

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

        params.gravity = Gravity.BOTTOM;

        RelativeLayout ly;
        final LayoutInflater inflater = (LayoutInflater) pcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ly = (RelativeLayout) inflater.inflate(R.layout.dialog, null);

        switch (state) 
        {
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("Call","RINGING");

            wm.addView(ly, params);
            break;

        case TelephonyManager.CALL_STATE_IDLE:

                Log.d("Call","End");
                //WindowManager wm = (WindowManager) pcontext.getSystemService(Context.WINDOW_SERVICE);

                if(ly!=null)
                {
                    wm.removeView(ly);
                    ly = null;
                }
            break;
        default:
            break;
        }
        super.onCallStateChanged(state, incomingNumber);
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_NONE);
    }


}       
}

The addView works fine, Here is the log

"View not attached to window manager"

08-24 20:05:56.404: W/DEBUG(28001): IDLE
08-24 20:05:56.424: D/Call(28001): End
08-24 20:05:56.424: D/AndroidRuntime(28001): Shutting down VM
08-24 20:05:56.424: W/dalvikvm(28001): threadid=1: thread exiting with uncaught exception (group=0x412982a0)
08-24 20:05:56.444: E/AndroidRuntime(28001): FATAL EXCEPTION: main
08-24 20:05:56.444: E/AndroidRuntime(28001): java.lang.IllegalArgumentException: View not attached to window manager
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:673)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:369)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at com.androidexample.broadcastreceiver.IncomingCall$CustomPhoneStateListener.onCallStateChanged(IncomingCall.java:105)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.os.Looper.loop(Looper.java:137)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at java.lang.reflect.Method.invoke(Method.java:511)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-24 20:05:56.444: E/AndroidRuntime(28001):    at dalvik.system.NativeStart.main(Native Method)
08-24 20:08:22.669: I/Process(28001): Sending signal. PID: 28001 SIG: 9

I had tried creating the layout programmaticaly too.. but no luck

Also can't figure out the id of the generated layout

Brycebryn answered 24/8, 2013 at 14:54 Comment(7)
i've never tried it directly via a broadcastReceiver. i've always opened a new activity, and there i've shown the overlay view. please try it out. here's a snippet of how to show an on-top view i've recently written : #18425113Cosmo
But will that be able to finish the activity from another state of broadcast receiverBrycebryn
I know I could remove the view on destroy of the activityBrycebryn
i don't understand. why would you want to show a view on top if you wish to remove it right away? if you wish to allow things around it to be clickable, that's how you do it (and you can finish the activity and it should still show on top) . in any other case, you can simply show a dialog on the activity and finish the activity when it's dismissed. alternativly , you can make the activity look like a dialog.Cosmo
The requirement is toBrycebryn
Add the overlay on ringing state and to remove it in idle stateBrycebryn
ok, so you need to launch a foreground service that will hold your app (making it alive for the time being), and an activity that will show the UI and finish itself once it has attached a view to the window. i've done the exact same thing in the past (even with a rounded image). it should work. i've even made it possible to drag it around . only problem i had is animations, but it might be because i had to make it work even on pre-honeycomb versions of android.Cosmo
B
3

The problem was, we cant use dialogs in broadcast receiver and it has to be in another activity that could be started with intent.

Brycebryn answered 27/8, 2013 at 12:9 Comment(0)
V
1

This method is called when the BroadcastReceiver is receiving an Intent broadcast. During this time you can use the other methods on BroadcastReceiver to view/modify the current result values. This method is always called within the main thread of its process, unless you explicitly asked for it to be scheduled on a different thread using registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler). When it runs on the main thread you should never perform long-running operations in it (there is a timeout of 10 seconds that the system allows before considering the receiver to be blocked and a candidate to be killed). You cannot launch a popup dialog in your implementation of onReceive().

The Android Documentation clearly says it.

Android Documentation on BroadcastReceiver

Vonnie answered 28/8, 2013 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.