Launch Window from the Android background

0

I am trying to launch a window when an action is executed on my app. The problem of this scheme can sometimes be executed in the background, which is why sometimes I will have to launch it without an activity:

I am running this fragment:

public void alertaSubscriptor(String titulo,String mensaje) {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSPARENT);

        final WindowManager wm = (WindowManager) MOCA.getApplicationContext().getSystemService(WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) MOCA.getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        View myView = inflater.inflate(R.layout.ventanaemrgente, null);
        myView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "touch me");
                wm.removeViewImmediate(v);
                return true;
            }
        });

        // Add layout to window manager
        wm.addView(myView, params);

    }

But in the background it does not run

  

02-16 09: 33: 07.140 31071-32472 / com.procibernetica.moca W / MessageQueue:   Handler (android.view.Choreographer $ FrameHandler) {21c0847c} sending   message to a Handler on a dead thread                                                                          java.lang.RuntimeException: Handler   (android.view.Choreographer $ FrameHandler) {21c0847c} sending message   to a Handler on a dead thread                                                                              at android.os.MessageQueue.enqueueMessage (MessageQueue.java:336)                                                                              at android.os.Handler.enqueueMessage (Handler.java:626)                                                                              at android.os.Handler.sendMessageAtTime (Handler.java:595)                                                                              at   android.view.Choreographer $ FrameDisplayEventReceiver.onVsync (Choreographer.java:751)                                                                              at   android.view.DisplayEventReceiver.dispatchVsync (DisplayEventReceiver.java:139)                                                                              at android.os.MessageQueue.nativePollOnce (Native Method)                                                                              at android.os.MessageQueue.next (MessageQueue.java:138)                                                                              at android.os.Looper.loop (Looper.java:131)                                                                              at android.os.HandlerThread.run (HandlerThread.java:61)

How could I throw a window or a bubble as a messenger, when the activity is in the background?

    
asked by Daniel ORTIZ 16.02.2017 в 15:38
source

2 answers

1

It is the idea of the background that activities with gui do not run. To have code execution in the background, use a Service . If you want your service to continue running in the background, you have to implement it with:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

Service.START_STICKY warns the system not to stop the service when the app ends.

    
answered by 16.02.2017 в 16:37
1

Launching a window in the background as a dialogue would not be useful because it would not be visible, in this case the ideal is to validate if a dialogue is shown or not:

private static boolean isForeground;

@Override
    protected void onResume() { 
        isForeground = true; //app regresa de background.
        super.onResume();
    }

    @Override
    protected void onPause() {
        foreground = false; //App en background.
        super.onPause();
    }

By means of the methods onResume() and onPause() , you can determine if it is foreground or background and by this show the dialogue.

if (isForeground) {
    // Muestra dialogo.
}

If you want a notification that can be sent when your Activity is in Background, it can be a push notification or a Toast, even a view like the one you mention but it is important to have the context. If the notification is sent from a service, the context would actually be the service.

    
answered by 16.02.2017 в 17:02