从服务中显示alertdialog

时间:2014-02-06 05:13:13

标签: android android-alertdialog

我需要从服务中显示AlertDialog并且我在服务上使用代码:

public void onCreate() {
        Intent i=new Intent(getApplicationContext(), GroupActivity.class);
        AlertDialog.Builder alert=new Builder(getApplicationContext());
        alert.setTitle("Success!");
        alert.setMessage("Hien tai");
        alert.setPositiveButton("Success", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alert.show();
    };

但是代码抛出了execption。 怎么做?

1 个答案:

答案 0 :(得分:0)

public void onCreate() {
    Intent i=new Intent(getApplicationContext(), GroupActivity.class);
    AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("Success!")
                .setMessage("Hien tai")
                .setPositiveButton("Success", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).create();

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

请注意,您必须使用以下权限:

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