In previous post,
we known how to open new class from notification, now we want to update service
from notification.
When user touch
to delete notification, we quit service.
Create a class
name NotificationDeleteReceiver.
public class NotificationDeleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent) {
context.stopService(new Intent(context,service.class));
}
}
In Manifest.xml, add
this line to above last </application>.
<receiver android:name="NotificationDeleteReceiver"
/>
After that in
class we send notification, copy this function to inside.
public void noti(String title, String message) {
Intent
intent = new Intent(this, NotificationDeleteReceiver.class);
intent.putExtra("com.example.vidu", 1);
PendingIntent
resultPendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(),
1, intent,
0);
NotificationCompat.Builder
b = new NotificationCompat.Builder(this);
b.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Noted!")
.setContentTitle(title)
.setContentText(message)
.setOngoing(true).setContentIntent(resultPendingIntent).setAutoCancel(true);//addAction(R.drawable.ic_launcher,
"Dismiss", bt);
NotificationManager
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1338, b.build());
}
When create notification, we use
function like this.
noti("Attention!","Something
need focus");
Now when user
touch to delete notification, running service will stop.
No comments:
Post a Comment