I have created a service which displays a status bar notification after certain time interval.
I also have created broadcast receiver which starts the service when phone restarts or power on. The problem I am facing is that when phone restarts , I see the notification in bar, but after that the application launches. I don’t want the application to launch itself, it should only launch when user clicks on the notification.
My code for Broad Cast Receiver:
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
try
{
Intent intent1 = new Intent(context, NotificationService.class);
context.startService(intent1);
}
catch(Exception e)
{
}
}
}
My code for notification is :
public static void showNotification(Context context )
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Pull Me Down!!", 1000);
Intent intent = new Intent(context,X.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
notification.setLatestEventInfo(context, "I came!!", "My First Notifcation" , pendingIntent);
notificationManager.notify(MY_ID, notification);
}
I am calling above method in onCreate of my Service. and also calling it in my X activity class:
NotificationService.setActivity(StatusBarNotificationActivity.this);
startService(new Intent(getApplicationContext(), NotificationService.class));
But don’t know why when phone starts notification is show but after few seconds the X activity also launches.
Solved it, just declared the
NotificationManagerandNotificationoutside the method, and it worked.