0

Am trying to create a dialog when i receive a certain type of message in my class which extends 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();
        }

but am getting an exception java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

at this line final AlertDialog alertDialog = builder.create();

My Main activity

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
}

My manifest.xml

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

My style resource

<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>

Where am i doing it the wrong way?

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • Why are you trying to use AppCompatActivity (which is used for activities with an action bar) when you have a NoActionBar theme? – Tim Dec 02 '15 at 09:36
  • @TimCastelijns because i need to `setSupportActionBar` and i can't do it by extending Activity – Edijae Crusar Dec 02 '15 at 09:38
  • @TimCastelijns or is there way i can `setSupportActionBar` without extending `AppCompatActivity` – Edijae Crusar Dec 02 '15 at 09:44
  • Normally, when you want to use the action bar, you use a theme other than NoActionBar, looking at the name, the reason should be obvious – Tim Dec 02 '15 at 09:46

1 Answers1

0

I had to extend AppCompatActivity in my activity because i wanted to use setSupportActionBar() method to set my toolbar as the actionbar.

To solve the problem, i did some changes in my class that extends GcmListenerService

i changed

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

to

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

This is my class that extends gcmlistener service

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();
    }

` my style used by my activity and my 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>
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • Getting the following error: "Unable to add window android.view.ViewRootImpl$W@2728b31 -- permission denied for this window type" any suggestions? – Moe Howard Dec 16 '15 at 19:11
  • Ah got it: Cool! Worked for me... Needed to add: as noted by this dude: http://stackoverflow.com/questions/18400539/windowmanagerbadtokenexception-unable-to-add-window – Moe Howard Dec 16 '15 at 19:17
  • @MoeHoward well i was far away, but when i returned, i have found that you solved the problem. great. if this answer helped you, don't forget to upvote it :) – Edijae Crusar Dec 17 '15 at 06:10