-3

In my application, I want to use dialog into service and for this, I wrote the code below, but after I click on the button to show dialog, I get a Force close error!

My service code:

public class FloatingLayoutService extends Service implements StepperFormListener {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //Inflate the layout using LayoutInflater
        layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        floatingLayout = (ConstraintLayout) layoutInflater.inflate(R.layout.service_floating_layout, null);

        floatingLay_root = floatingLayout.findViewById(R.id.floatingLay_root);
        floatingLay_main = floatingLayout.findViewById(R.id.floatingLay_main);
        floatingLay_emptyImg = floatingLayout.findViewById(R.id.floatingLay_emptyImg);
        ...

        //Set layout params to display the controls over any screen.
        int LAYOUT_FLAG;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                LAYOUT_FLAG,
                0,
                PixelFormat.TRANSLUCENT);
        // From API26, TYPE_PHONE deprecated. Use TYPE_APPLICATION_OVERLAY for O
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        //Initial position of the floating controls
        params.gravity = Gravity.BOTTOM | Gravity.START;
        params.x = 0;
        params.y = 0;
        //Add the controls view to windowManager
        windowManager.addView(floatingLayout, params);

        //Task page btn
        floatingLay_infoContentNextPage.setOnClickListener(v -> {
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

            alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            alertDialog.show();

        });

        return START_STICKY;
    }

Force close error :

Process: com.app, PID: 32088
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
    at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)
    at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:94)
    at android.support.v7.app.AlertController.installContent(AlertController.java:232)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:279)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:475)
    at android.app.Dialog.show(Dialog.java:374)
    at com.app.tester.tests_steps.FloatingLayoutService$1.onAnimationEnd(FloatingLayoutService.java:326)
    at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
    at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1293)
    at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
    at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
    at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
    at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:977)
    at android.view.Choreographer.doCallbacks(Choreographer.java:791)
    at android.view.Choreographer.doFrame(Choreographer.java:723)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:965)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6710)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

Show me error for this line : alertDialog.show();

How can i fix it?

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Dr.KeyOk
  • 1
  • 4

2 Answers2

0

You cannot show dialogs in a Service. You need an Activity Context for dialogs.

With a Service Context you can start a new Activity and display your dialog there.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Thanks my friend, but how can i show dialog?! can you send to me code with my above codes? please my friend – Dr.KeyOk Mar 11 '20 at 12:24
0

Go to res/values/styles.xml

Below your "AppTheme" style closing bracket, add this:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">#ff0000</item>
    <item name="colorAccent">#ff0000</item>
    <item name="android:background">#00FFFFFF</item>
</style>

And then, modify the way you create your AlertDialog:

AlertDialog.Builder alertdialogobuilder = new AlertDialog.Builder(this, R.style.MyDialogTheme);

Hope it helps!

Juan Sancho
  • 321
  • 1
  • 7
  • Thanks my friend, after add your code show me below error : `android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$WEx@a9c2c6a -- permission denied for window type 2003` – Dr.KeyOk Mar 11 '20 at 12:28
  • This error means you are trying to launch the dialog above the lock screen or in another context outside the application right? – Juan Sancho Mar 11 '20 at 14:26