Application keep crashing, when i try to show() AlertDialog from a ForegroundService

71 views Asked by At

I try to build an app, where you type the Username and Pass in the MainActivity, which will start a ForegroundService and in an endless loop keep sending User and Pass to a PhP server.

App is working fine, keep receiving the response and service running with app closed.

But if the response contains a specific number, lets say 1, i would like a pop up window to appear. AlertDialog as i think you cant close that if you click next to it. I need the pop to stay open, until the user doesnt click on the Acknowledge button.

private void alert(){
    Ringtone ringtone = RingtoneManager.getRingtone(this,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
    ringtone.play();
    AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
    builder1.setTitle("ACTION REQUIRED");
    builder1.setMessage("+message here+");
    builder1.setCancelable(false);

    builder1.setPositiveButton(
            "Acknowledge",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ringtone.stop();
                    dialog.cancel();
                }
            });

    AlertDialog alert11 = builder1.create();
    alert11.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alert11.show();

}

Above code is in the Foreground Service. onStartCommand call the method which makes the request. onResponse will call the above code if its get the number 1. But app keep stopping/chrashing when its get to the alert11.show(); part.

On the virtual device i gave permission to the app to draw over... In the Manifest file i have:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Please help. Thank you.

0

There are 0 answers