1

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.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Dens
  • 125
  • 11
  • Your best shot is to use ForegroundService and display a notification instead of popup window. – Rahul Khurana Aug 13 '19 at 05:11
  • how i can do, can you provide some reference. i don't want open activity and display popup or dialog window is it possible ? – Dens Aug 13 '19 at 05:14
  • you can try a hack approach and make an activity look like a dialog with transparent background and stuff and launch that – Kushan Aug 13 '19 at 05:17
  • I'm afraid I think it's not possible – Rahul Khurana Aug 13 '19 at 05:18
  • i also think it is not possible and yes @Kushan i tried this but ask me to change client requirement – Dens Aug 13 '19 at 05:23
  • but how will the client know that this is not a dialog? make it look like a dialog – Kushan Aug 13 '19 at 05:44
  • he show me note application and set reminder and kill application. it is displaying popup dialog with 2 button option dismiss and goto note activity and click on dismiss. insulting – Dens Aug 13 '19 at 05:50
  • @RahulKhurana Worked .. got solution we can open dialog after killing application – Dens Aug 13 '19 at 09:19
  • @Dens that's great. Would you mind sharing the link so it would be helpful to others – Rahul Khurana Aug 13 '19 at 09:23
  • 1
    of course... but still i have some issue with dialog automatically closed after sometime.. after completing i will post code . – Dens Aug 13 '19 at 09:27
  • Were you able to find solution ?? If yes, can you please share the code. I also want to dialog from a foreground service. – K Pradeep Kumar Reddy May 01 '20 at 06:47

2 Answers2

2

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)) {

            }
        }
    }
Nimesh Patel
  • 1,394
  • 13
  • 26
0

Maybe you can change the type.

LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
igarasi
  • 137
  • 7
  • I found another answer. [link](https://stackoverflow.com/questions/3599563/alert-dialog-from-android-service/33572463#33572463) – igarasi Aug 13 '19 at 06:49
  • partially your answer helpful. i forgot to add ettings.canDrawOverlays(this). now i dialog is open. but it is closed after 15 second – Dens Aug 13 '19 at 09:18