无法在我的类中启动扩展GcmListenerService的对话框

时间:2015-12-02 09:32:26

标签: android android-activity google-cloud-messaging android-dialogfragment android-dialog

当我在班级收到某种类型的邮件时尝试创建一个对话框,该邮件扩展为GcmListenerService

public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {

    final String senderName = data.getString("sender");
    final String message = data.getString("message");

    if(senderName != null && senderName.equals("XXX")){
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message)
                .setTitle("Alert!")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                        dialog.dismiss();
                    }
                });
        try {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    final AlertDialog alertDialog = builder.create();
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    alertDialog.show();
                    Looper.loop();

                }
            }.start();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

但是我遇到了异常 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

此行 final AlertDialog alertDialog = builder.create();

我的主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar =(Toolbar) findViewById(R.id.mainActivityBar);
     setSupportActionBar(toolbar);
    //other statements and methods here
}

我的manifest.xml

 <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/mainActivityTheme">
    </activity>

我的风格资源

<style name="mainActivityTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="android:windowActionBar">false</item>
</style>

我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

我必须在我的活动中扩展AppCompatActivity,因为我想使用setSupportActionBar()方法将我的工具栏设置为操作栏

为了解决这个问题,我在课堂上做了一些扩展GcmListenerService

的修改

我改变了

final AlertDialog.Builder builder = new AlertDialog.Builder(this);

final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.mainActivityTheme));

这是我的扩展gcmlistener服务的类

public class MyGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
final String senderName = data.getString("sender");
final String message = data.getString("message");

if(senderName != null && senderName.equals("XXX")){
   final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.mainActivityTheme));

    builder.setMessage(message)
            .setTitle("Alert!")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                    dialog.dismiss();
                }
            });
    try {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                final AlertDialog alertDialog = builder.create();
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                alertDialog.show();
                Looper.loop();

            }
        }.start();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

` 我的活动和我的AlertDialog.Builder使用的风格

<style name="mainActivityTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="android:windowActionBar">false</item>
</style>