Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4393

Dialog box crashes Android service

CODE

         this.getApplicationContext().getContentResolver().registerContentObserver(
              android.provider.Settings.System.CONTENT_URI, 
              true, new ContentObserver(new Handler()) {
                public void onChange(boolean selfChange) {
                  Toast.makeText(audioServices.this, "Working..", Toast.LENGTH_SHORT).show();
                  //dispVC();
                  dialog = new Dialog(audioServices.this);
                    dialog.setContentView(R.layout.vc);

                    // set the custom dialog components - text, image and button
                    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                      dialog.show();
                  //System.out.println("Works!");
                }
            });

This is the logcat. Note - 01-09 17:54:43.137: E/AndroidRuntime(7210): at com.torcellite.popupvc.audioServices$1.onChange(audioServices.java:59)

Line 59 is dialog.show();

---EDIT---

So, I changed the code to this.

Intent dialogIntent = new Intent(audioServices.this, vcDialog.class);
                    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    audioServices.this.startActivity(dialogIntent);

My app still crashes. This is the logcat.

Upvotes: 1

Views: 510

Answers (3)

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

dialogIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

put these lines instead of

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

line and try calling activity like this

this.getApplicationContext().startActivity(dialogIntent)

Upvotes: -1

Raghav Sood
Raghav Sood

Reputation: 82563

A service doesn't have any UI elements, so can't show a dialog box. A dialog can only be added from an Activity context. You can either call an activity that does have a UI, with a dialog theme if you'd like or it might be better to create a notification which is the preferred option for Android alerts.

EDIT

Based on your new code, your intent is fine. Instead add the following to your manifest in the application tag:

<activity android:name=".vcDialog" />

Upvotes: 2

Gaurav Vashisth
Gaurav Vashisth

Reputation: 7737

Use Activity context instead of context of Service. Also instead of dialog use DialogFragments.

Upvotes: 0

Related Questions