0

I use GCM for my Android app. To receive messages I do the following:

public class MyGcmListenerService extends GcmListenerService {

  @Override
  public void onMessageReceived(String from, Bundle data) {
    Intent dialogIntent = new Intent(this, DialogActivity.class);
    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    dialogIntent.putExtra(DialogActivity.TITLE, title);
    dialogIntent.putExtra(DialogActivity.MESSAGE, message);
    startActivity(dialogIntent);
  }

}

When I run the above code I get the following error:

10-19 17:08:48.446 8657-8657/? E/AndroidRuntime: FATAL EXCEPTION: main
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.majestella/de.majestella.gcm.DialogActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.access$700(ActivityThread.java:134)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4867)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:  Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:275)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.Activity.requestWindowFeature(Activity.java:3264)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at de.majestella.gcm.DialogActivity.onCreate(DialogActivity.java:24)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5047)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.access$700(ActivityThread.java:134) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4867) 
10-19 17:08:48.446 8657-8657/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 

How can I show an Alert Dialog when onMessageReceived() is called in case the app is in foreground?

Michael
  • 32,527
  • 49
  • 210
  • 370

2 Answers2

0

Start an activity that has a theme of a dialog

<activity android:theme="@android:style/Theme.Dialog" />

Android Activity as a dialog

Or

you will have to declare a broadcast receiver in all your activities and send a broadcast from your service to trigger and activity to show the dialog.

in onPause of each activity stop the broadcast receiver for that activity then you know you will only get alerted when one of your activities is in the foreground

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • How do I start such an activity from the Service? – Michael Oct 19 '15 at 14:42
  • you start an activity the same way you would any other activity – tyczj Oct 19 '15 at 14:43
  • I want to do this only if my app is in foreground. – Michael Oct 19 '15 at 14:45
  • you can still do that or you have to declare a broadcast receiver in every activity and send a broadcast when a new message comes in. either way there is extra logic you have to handle to see if you app is running, you cannot show ui from a service – tyczj Oct 19 '15 at 14:49
  • the code you posted is not the problem, please read the error and you will see where the problem is. i assume you are doing something with the dialog before showing it and you didnt call request feature first `requestFeature() must be called before adding content` – tyczj Oct 19 '15 at 15:13
  • Now I get ``You need to use a Theme.AppCompat theme (or descendant) with this activity`. Any idea? – Michael Oct 19 '15 at 15:22
  • you probably need to use whatever the dialog theme is for appcompat, i am not sure what that is though – tyczj Oct 19 '15 at 15:25
  • maybe try this `Theme.AppCompat.Light.Dialog` – tyczj Oct 19 '15 at 15:27
0

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

Check your DialogActivity, and call requestFeature() before setContent

Noureddine AMRI
  • 2,942
  • 1
  • 21
  • 28