1

When the app receives a push message, I coded onMessage to launch the MainActivity with a alert dialog to popup the message. So far so good.

What I really want is the popup only! So when the user is busy with another app, it only shows a dialog with the options to cancel or launch app...

GCMIntentService part

@Override
    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "Received a message");
        String message = intent.getExtras().getString("price");
        displayMessage(context, message);
        generateNotification(context, message);
        //Launch a activity
        Intent i = new Intent();
        i.putExtra("message", message);
        i.setClassName("nl.easy.winkel", "nl.easy.winkel.MainActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
Harry
  • 768
  • 1
  • 7
  • 26

1 Answers1

3

One thing you can do is to make the MainActivity background transparent using approach defined in this post (copying below for quick reference) so that the user would feel that the popup is over the current activity she is using and finish() the activity if user selects cancel...

Making Activity Background transparent:

Add the following style In your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

(the value @color/transparent is the color value #00000000 which I put in res/values/color.xml file. You can also use @android:color/transparent in later Android versions)

Then apply the style to your activity, for example:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
Community
  • 1
  • 1
Praful Bhatnagar
  • 7,450
  • 2
  • 35
  • 44
  • Thanks, I followed your link. A good solution! The only thing I have to figure out: the dialog has two buttons: launch and cancel, but when the user is allready in app I like a OK button only. So, is there a way to obtain if app in background or on top, so I can adjust the buttons? Thanks from Holland! – Harry Nov 24 '12 at 21:24
  • check out following link http://stackoverflow.com/questions/3667022/android-is-application-running-in-background/5862048#5862048 this might help... – Praful Bhatnagar Nov 24 '12 at 21:33
  • Hmm, I followed the procedure but the app gives onPause when the alert dialog pops up and so the adjust of buttons fails. – Harry Nov 25 '12 at 13:34
  • try moving the call to `MyApplication.activityPaused();` from activities `onPause()` to activities `onStop()` method. Now since the dialog activity is transparent the back activity (current activity before the GCM notification) `onStop()` method would jot be called only the `onPause()` would be called... – Praful Bhatnagar Nov 26 '12 at 10:01
  • I have to test it and save the logcat because it's not adjusting yet. Another question comes up: when de message dialog shows, waiting onClickListener and i use the back button on phone, the app crashes... how can a catch this? What is the catch formula? Thx. – Harry Nov 26 '12 at 19:46
  • i tested it and it was working fine at my end. Only the `onPause()` was getting called if the new activity is transparent.. Please post this checking for app running as a separate question so that other people can see it and help you out... also add the dialog back press crash logs to that question.. – Praful Bhatnagar Nov 27 '12 at 05:00
  • Strange, i will go further with it. – Harry Nov 27 '12 at 08:31