Android- start a dialog when application on background

265 views Asked by At

I have an application which has call dialog. I'm trying to show call dialog on lock screen. it has worked when application was not on background, actually it didn't work when application was open and lock screen was active.
Here is my code:

    public class CustomMainActivity extends QtActivity implements SensorEventListener
    {

    @Override
    protected void onStart() {
        super.onStart();
        if (getCallId() != 0)
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        else
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    .
    .
    .
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (getCallId() != 0)
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        else
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    .
    .
    .
    }

}
1

There are 1 answers

4
Daud Arfin On

Try adding these flags inside onCreate().

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    super.onCreate(savedInstanceState);
}