Saturday, April 23, 2011

Update Notification

Each notification is uniquely identified by the NotificationManager with an integer ID, the notification can be revised by calling setLatestEventInfo() with new values, change some field values of the Notification, and then call notify() again.

It's a exercise demonstrate how to updte notification, modified from exercise "Start a service to send Notification".

Update Notification

The service will be started when App start. To make it clear to understand, two buttons are implemented to request service to send notification using different Notification ID. Use enter target URL in the EditText field, and click any button to send notification, it will be send out and update notification with the ID.

Layout, main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="\nService Start in App Start\n"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Target address to open in Browser"
/>
<EditText
android:id="@+id/targetaddr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendnotification_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send Notification ID 1"
/>
<Button
android:id="@+id/sendnotification_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send Notification ID 2"
/>
<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Service"
/>
</LinearLayout>


Main activity, AndroidNotifyService.java
package com.exercise.AndroidNotifyService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidNotifyService extends Activity {

EditText targetAddr;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonStopService = (Button)findViewById(R.id.stopservice);
Button buttonSendNotification1 = (Button)findViewById(R.id.sendnotification_1);
Button buttonSendNotification2 = (Button)findViewById(R.id.sendnotification_2);
targetAddr = (EditText)findViewById(R.id.targetaddr);

//Start the service in App start, instead of clicking button
Intent intent = new Intent(AndroidNotifyService.this, com.exercise.AndroidNotifyService.NotifyService.class);
AndroidNotifyService.this.startService(intent);

buttonStopService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra("RQS", NotifyService.RQS_STOP_SERVICE);
sendBroadcast(intent);
}});

buttonSendNotification1.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra("RQS", NotifyService.RQS_SEND_NOTIFICATION_1);
intent.putExtra("TARGET", targetAddr.getText().toString());
sendBroadcast(intent);
}});

buttonSendNotification2.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra("RQS", NotifyService.RQS_SEND_NOTIFICATION_2);
intent.putExtra("TARGET", targetAddr.getText().toString());
sendBroadcast(intent);
}});

}
}


The service, NotifyService.java
package com.exercise.AndroidNotifyService;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;

public class NotifyService extends Service {

final static String ACTION = "NotifyServiceAction";
//final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
final static int RQS_SEND_NOTIFICATION_1 = 2;
final static int RQS_SEND_NOTIFICATION_2 = 3;

NotifyServiceReceiver notifyServiceReceiver;

private static final int MY_NOTIFICATION_ID_1=1;
private static final int MY_NOTIFICATION_ID_2=2;
private NotificationManager notificationManager;
private Notification myNotification;

Context myContext;
String myNotificationTitle = "Exercise of Notification!";

@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);

// Send Notification
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.icon,
"Notification!",
System.currentTimeMillis());
myContext = getApplicationContext();
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public class NotifyServiceReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE){
stopSelf();
}else if(rqs == RQS_SEND_NOTIFICATION_1){
String myTarget = arg1.getStringExtra("TARGET");
SendNotification(MY_NOTIFICATION_ID_1, myTarget);

}else if(rqs == RQS_SEND_NOTIFICATION_2){
String myTarget = arg1.getStringExtra("TARGET");
SendNotification(MY_NOTIFICATION_ID_2, myTarget);
}
}

private void SendNotification(int id, String target){
String notificationText = target;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(target));
PendingIntent pendingIntent
= PendingIntent.getActivity(myContext,
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);

myNotification.setLatestEventInfo(myContext,
myNotificationTitle,
notificationText,
pendingIntent);
notificationManager.notify(id, myNotification);
}
}

}


AndroidManifest.xml, to include NotifyService.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidNotifyService"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidNotifyService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotifyService"/>
</application>
</manifest>


Download the files.

No comments:

Post a Comment