Tuesday, March 26, 2013

Generate Notification in IntentService

Last exercise demonstrate how to "Send data from IntentService to Activity, via additional Broadcast". We can also generate Notification in IntentService, to inform user about the progress.

Generate Notification in IntentService

Generate Notification in IntentService


Modify MyIntentService.java from last exercise.
package com.example.androidintentservice;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class MyIntentService extends IntentService {
 
 private static final int MY_NOTIFICATION_ID=1;
 NotificationManager notificationManager;
 Notification myNotification;
 
 public static final String ACTION_MyIntentService = "com.example.androidintentservice.RESPONSE";
 public static final String ACTION_MyUpdate = "com.example.androidintentservice.UPDATE";
 public static final String EXTRA_KEY_IN = "EXTRA_IN";
 public static final String EXTRA_KEY_OUT = "EXTRA_OUT";
 public static final String EXTRA_KEY_UPDATE = "EXTRA_UPDATE";
 String msgFromActivity;
 String extraOut;

 public MyIntentService() {
  super("com.example.androidintentservice.MyIntentService");
 }

 @Override
 protected void onHandleIntent(Intent intent) {
  
  //get input
  msgFromActivity = intent.getStringExtra(EXTRA_KEY_IN);
  extraOut = "Hello: " +  msgFromActivity;
  
  for(int i = 0; i <=10; i++){
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   //send update 
   Intent intentUpdate = new Intent();
   intentUpdate.setAction(ACTION_MyUpdate);
   intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
   intentUpdate.putExtra(EXTRA_KEY_UPDATE, i);
   sendBroadcast(intentUpdate);
   
   //generate notification
   String notificationText = String.valueOf((int)(100 * i / 10)) + " %";
   myNotification = new NotificationCompat.Builder(getApplicationContext())
   .setContentTitle("Progress")
   .setContentText(notificationText)
   .setTicker("Notification!")
   .setWhen(System.currentTimeMillis())
   .setDefaults(Notification.DEFAULT_SOUND)
   .setAutoCancel(true)
   .setSmallIcon(R.drawable.ic_launcher)
   .build();
   
   notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
  }
  
  //return result
  Intent intentResponse = new Intent();
  intentResponse.setAction(ACTION_MyIntentService);
  intentResponse.addCategory(Intent.CATEGORY_DEFAULT);
  intentResponse.putExtra(EXTRA_KEY_OUT, extraOut);
  sendBroadcast(intentResponse);
 }

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
  notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
 }

}


download filesDownload the files.

Related:
- error of using NotificationCompat.Builder, IllegalArgumentException: contentIntent required

Next:
- Stop IntentService, and know about the life-cycle of IntentService.

No comments: