Search code examples
androidandroid-serviceandroid-alertdialog

how to show dialog window from background service without open application Activity


Hey I am developed Reminder service application. i want to show popup when application background service notify me. without interfering my application activity or open activity display popup and i can cancel/close popup.

Unfortunately i tried many methods but i am unable to perform this event to show dialog with non Activity class. now i don't have any error on my code. it is toast where i initiated view. it shows message waiting for foreground, but i don't want to open activity and show popup. how i can do this possible help me out please. here i am posting my service code.

public class StartAlaramService extends Service {

private WindowManager mWindowManager;
private WindowManager.LayoutParams mParams;
private View mRootView;
private Context mContext = null;
private LayoutInflater mInflater = null;
private View alertView = null;


@Override
public void onCreate() {
    super.onCreate();
    mContext=this;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    try{

        if (null != mWindowManager) {
            if (null != alertView) {
                mWindowManager.removeView(alertView);
            }
            mWindowManager = null;
            mParams = null;
            mInflater = null;
            alertView = null;
        }

    } catch (Exception ex){
        ex.printStackTrace();
        Log.e("SERVICE ", " Code Error");
    }


    showView();
    return StartAlaramService.START_NOT_STICKY;

}

private void initState() {

    Log.e("SERVICE ", "Intiate View State");


    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }

        mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);


    if (null == mWindowManager) {
        mWindowManager = ((WindowManager) mContext.getSystemService(WINDOW_SERVICE));
    }
}

private void showView() {


    try{

        initState();
        // Create the view
        mRootView = initView();
        // Show the view
        mWindowManager.addView(mRootView, mParams);
        // Do some extra stuff when the view's ready


    } catch (Exception ex){
        ex.printStackTrace();
        Log.e("SERVICE ", " EXCEPTION " + ex.getMessage());
    }

}

private View initView() {

        mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        mWindowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
        LayoutInflater li = LayoutInflater.from(mContext);
        setTheme(R.style.custom_dialog);
        alertView = li.inflate(R.layout.custom_layout_notification, null);


    return alertView;
}

}

SYSTEM ALERT PERMISSION already defined in Manifest.


Solution

  • To Resolve SYSTEM ALERT PERMISSION You have to get permission of DrawOverlays. You can get this permission through following two steps.

    Step 1: Put below code in Main activity which check whether permission is given or not if not then then it will ask for permission.

    if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }
    

    Step 2: Then after Add below code in onActivityResult.

    if (Build.VERSION.SDK_INT >= 23) {
            if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
                if (Settings.canDrawOverlays(this)) {
    
                }
            }
        }