среда, 17 августа 2011 г.

Android: Notification in your application (example).




Here we'll see how to make your application notify user to do something with it, even when it's off. Most of tutorials show notifications after click on the button or something like that, so - are useless, because it usually makes sense to notify user when application activity is not working.
Notification constructor has "when" parameter, notification builder has setWhen method. But the truth is, when you call NotificationManager.notify, notification appears immediately. "When" is used only to sort notifications.

I need to remind user to run my application periodically... One way I found is to make this - use service and timer (or AlarmManager, but it's next story).

So, the first step is - create service.
    
public class NotifyService extends Service {
    // Notification interval. 
    private static final long UPDATE_INTERVAL = 1000*60*60*24;

    private Timer timer = new Timer();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        // we shedule task "showNotification" to run everyday.
        timer.scheduleAtFixedRate(
                new TimerTask() {
                    public void run() {
                        showNotification();
                    }
                },
                0,
                UPDATE_INTERVAL);
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public void onStart(Intent intent, int startid) {
    }

    public void showNotification() {
// Here we are going do something...later
    }
}
Don't forget add information about service in your manifest file.
Inside "application" tag put

<service android:enabled="true" android:name="NotifyService" />
Then we should start service from activity. I suggest to do it in
"onStart" method:

public void onStart() {
  super.onStart();
  notifyService = new Intent(this, NotifyService.class);
  startService(notifyService);
       
}
To stop service, use simple code:

notifyService = new Intent(this, NotifyService.class);
stopService(notifyService);
Do not put it into onPause e t.c., because your service will die when application is closed, that's not what we want. Maybe you can offer user special button "Stop notifications", or, better, check option - it's up to you.
In this example I just let it live forever.

You can see if your service is working in the list of active applications on your phone - close activity window, active application should remain.

Now, let's add notification code - fill showNotification method of our service

public void showNotification() {
  NotificationManager mManager = 
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  Notification notification = 
    new Notification(R.drawable.icon, "My App", System.currentTimeMillis());
  Intent intent = new Intent(this, MainActivity.class);
    notification.setLatestEventInfo(this, 
     "My App", 
     "Use me!",
      PendingIntent.getActivity(this.getBaseContext(), 
      0, 
      intent,
      PendingIntent.FLAG_CANCEL_CURRENT));
  notification.flags = Notification.DEFAULT_LIGHTS 
   | Notification.FLAG_AUTO_CANCEL;
  mManager.notify(1, notification);
}

Check it, must work!

P.S.
To notify a user in specified time requires some more logic in code above.
Also, there is a problem: some devices stop processors or reduce frequency, and timer does not work properly.
For long-period notifications, the right way is to use AlarmManager, I hope to write about it in my next post...





Комментариев нет: