简体   繁体   中英

Show Alert Dialog on GCM onMessageReceived?

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?

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

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

Check your DialogActivity, and call requestFeature() before setContent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM