1

代码

         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!");
                }
            });

这是logcat。笔记 -01-09 17:54:43.137: E/AndroidRuntime(7210): at com.torcellite.popupvc.audioServices$1.onChange(audioServices.java:59)

第 59 行是dialog.show();

- -编辑 - -

所以,我把代码改成了这个。

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

我的应用程序仍然崩溃。这是logcat

4

3 回答 3

2

服务没有任何 UI 元素,因此无法显示对话框。只能从 Activity 上下文添加对话框。您可以调用具有 UI 的活动,如果您愿意,可以使用对话框主题,或者最好创建一个通知,这是 Android 警报的首选选项。

编辑

根据您的新代码,您的意图很好。而是将以下内容添加到应用程序标记中的清单中:

<activity android:name=".vcDialog" />
于 2013-01-09T12:31:42.000 回答
0

使用活动上下文而不是服务上下文。也可以使用 DialogFragments 代替对话框。

于 2013-01-09T12:34:11.000 回答
-1
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

把这些行代替

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

行并尝试像这样调用活动

this.getApplicationContext().startActivity(dialogIntent)
于 2013-01-09T12:31:25.707 回答