1

In my android project I need to show the notification when the certain event has occurred. In my application, I have a service which starts at boot completion. And this Service starts the background threads. These threads raises the event. Now the requirement is, when the event occurred , i want to show the notification in the middle of the screen.this notification is similar to the dialog box with single button. The notification screen exist until the user clicked on the button,means when the user clicked on the button,the screen will disappear.I don't know how the create the notification in the middle of the screen.

Any help will be appreciated..

Balaji.K
  • 8,745
  • 5
  • 30
  • 39

2 Answers2

2

set this code in a method and call this method with passing data which you want to display.

this is android popup. it will display middle of the screen.

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
        alt_bld.setMessage("set target message data").setCancelable(false).setPositiveButton("yes", new OnClickListener() { 

 public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub 
} }).setNegativeButton("No", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { 
// TODO Auto-generated method stub dialog.cancel(); 
} });

 AlertDialog alert = alt_bld.create(); alert.setTitle("Popup button"); alert.show();

For more info, refer this: http://developer.android.com/reference/android/app/Dialog.html ,

Siten
  • 4,515
  • 9
  • 39
  • 64
  • This code may in the end come useful for the problem, but can't be called directly from the Service. – kabuko Jun 18 '11 at 06:51
1

A Service can't create UI, so you'll need to create an Activity that contains the UI you want, and send an Intent from the Service to the Activity.

Update:

I really recommend that you start from the fundamentals to understand everything that's going on here, but basically you need to:

  1. Create an activity that looks like the UI you want
  2. Send an intent to start this activity from your service

For #1, you have two obvious options that I can think of, either of which should work:

For #2, this is basic Android:

startActivity(new Intent(this, ActivityClass.class));
Community
  • 1
  • 1
kabuko
  • 36,028
  • 10
  • 80
  • 93
  • iam able to create the status bar notifications. but i don't know how to create the notification in the middle of the screen. Would you provide any link dear – Balaji.K Jun 18 '11 at 06:29
  • I'm quite surprised that the framework does not provide a DialogActivity, since it seems there is no other possibility (except the `Toast`) to display information from a service or a notification. – rds Jun 19 '11 at 17:49