0

What I want to create is a popup style application.

I have a service in the background - something arrives on the queue and i want an activity to start to inform the user - very very similar to the functionality of SMSPopup app.

So I have the code where something arrives on the queue and it calls my activity.

However for some reason the activity always shows on top of the originally started activity instead of just appearing on the main desktop of the android device.

As an example:

I have the main activity which is shown when the application is run

I have the service which checks queue

I have a popup activity.

When i start the main activity it starts the service - I can now close this.

I then have something on the queue and it creates the popup activity which launches the main activity with the popup on top of it :S How do I stop this and have it behave as i want...

The popup class is :




public class SMSPopup extends Activity implements OnClickListener{

 public static String msg;


 @Override
 public void onCreate(Bundle bundle){
  super.onCreate(bundle);
 // Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
  this.setContentView(R.layout.popup);
  TextView tv = (TextView)findViewById(R.id.txtLbl);
  Intent intent = getIntent();
  if (intent != null){
      Bundle bb = intent.getExtras();
      if (bb != null){
          msg = bb.getString("com.andy.tabletsms.message");
      }
  }

  if(msg == null){
   msg = "LOLOLOL";
  }
  tv.setText(msg);

  Button b = (Button)findViewById(R.id.closeBtn);
  b.setOnClickListener(this);
 }


 @Override
 public void onClick(View v) {
  this.finish();
 }
}




and I call the activity from a broadcast receiver which checks the queue every 30 seconds or so :


  if(main.msgs.size()>0){

      Intent testActivityIntent = new Intent(context.getApplicationContext(), com.andy.tabletsms.work.SMSPopup.class);
    testActivityIntent.putExtra("com.andy.tabletsms.message", main.msgs.get(0));
    testActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(testActivityIntent);

      }

The layout is here : http://pastebin.com/F25u6wdM

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130
  • What is 'main' (as in main.msgs.get(0)) and where is it? – Squonk Jan 07 '11 at 00:16
  • Its an arraylist which contains strings.... not really related to the problem :) – RenegadeAndy Jan 07 '11 at 00:25
  • So you say - "...launches the main activity with the popup on top of it". Has the main activity 'really' been launched? In other words, when you press the BACK button, the popup activity disappears and you see the main activity behind. Or is it the case that the popup activity has a background with the same title bar as the main activity making it look like the main activity? – Squonk Jan 07 '11 at 02:47

1 Answers1

3

This is against the design practice suggested by Android. See http://developer.android.com/guide/topics/ui/notifiers/notifications.html

A background Service should never launch an Activity on its own in order to receive user interaction.

You could show the message in a Toast and/or notification. From the notification, you could start a new intent.

GSree
  • 2,890
  • 1
  • 23
  • 25
  • Well plenty of apps do it - and its the type of notification I want for my application - and its for my own use only - so its up to me! – RenegadeAndy Jan 07 '11 at 00:31
  • And when i say "service" its not a service as defined by – RenegadeAndy Jan 07 '11 at 00:35
  • ok. I get it. I think you have a thread. And you want the thread to run even when the activity is not in forefront. right? If this is right, I dought if you can do this. I suggest transferring the control to a service(real service ... not thread) when the user clicks some button/action. When the service needs to popup a window, you could either create notification and/or a toast. Also.. (if you really wish) you could start an Activity from the same place (even though it is against android recommendation). – GSree Jan 07 '11 at 00:41
  • Regardless of all of that - i still need my question answered! My actvity starts - but has the original activity in the background which is not what I need / want – RenegadeAndy Jan 07 '11 at 00:46
  • Even if i did it via notification then they click that and it opens - i still want it to open in the foreground of the UI without any back panel or other activity in the background. – RenegadeAndy Jan 07 '11 at 00:52
  • See this thread. http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack .. might answer your query. – GSree Jan 07 '11 at 00:59
  • I think I am not getting the full question. "UI without any back panel or other activity in the background" Are you saying you dont want a full screen activity? Best if you use the Android terminology. Every UI must have an activity. So your comment that UI without an activity is confusing. – GSree Jan 07 '11 at 01:03
  • Yes the broadcast receiver exists...It receives every 30 seconds or so. There is no direct service I was wrong. Each 30 seconds a check is made for something on the queue. If there is I want to make a popup which would be a transparent activity which has no title and is not full screen - i.e you would see the icons + wallpaper around the ui if you had no other apps running. – RenegadeAndy Jan 07 '11 at 01:48
  • Check if you can create a widget and call a widget. Again I dont think this is best practice. But then again.. you are the sort of person who like to ignore best practices. :) – GSree Jan 07 '11 at 01:54