tags:

views:

174

answers:

2

in my app a background service starts and from that service i want to set Status bar notification, that the service has Started following is the code :

    Context context = getApplicationContext();
    String ns = Context.NOTIFICATION_SERVICE;
    int icon = R.drawable.icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(MyService.this, MyClass.class);
    notificationIntent.setFlags(  Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
   NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
   mNotificationManager.notify(1, notification);

Notification is displayed in Status bar But whin i click on that MyClass.class is not fired.And in log cat it shows "Input Manager Service Window already focused ignoring focuson ...."

so plz provide solution.

thanks in advance

+1  A: 

I'm not sure what you mean by "MyClass.class is not fired", but if it's already loaded and you're expecting onCreate to be called again, you may be expecting the wrong results. I'm doing something very similar and to get code to execute on my already running Activity-derived class from a notification touch, I pass PendingIntent.FLAG_UPDATE_CURRENT as the 4th parameter to the PendingIntent constructor

PendingIntent contentIntent = PendingIntent.getActivity(YourService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

and I put my code to execute in the following method of the Activity:

@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    // your code here

}
Rich
`FLAG_UPDATE_CURRENT` only impacts other `PendingIntents` registered for similar `Intents` (e.g., same action, same component). `onNewIntent()`, though, is important -- if the activity is already running, it will not receive `onCreate()`, since it has already been created.
CommonsWare
+1  A: 

First, do not use getApplicationContext(). Your Service is a Context, and the "application context" is generally useless for places where you need a Context.

Second, I doubt you need FLAG_ACTIVITY_NEW_TASK.

Here is a sample application showing the use of Notifications.

CommonsWare