Activity that runs in the foreground can send a message to the user via dialog, Toast, a program that runs later, such as a Service application such as downloading, receiving information, and then using Notification (notification) to send a message to the user.
1 Importandroid.app.Activity;2 Importandroid.app.Notification;3 ImportAndroid.app.NotificationManager;4 Importandroid.app.PendingIntent;5 ImportAndroid.content.Context;6 Importandroid.content.Intent;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One Importandroid.widget.RemoteViews; A - Public classNotificationactivityextendsActivity { - the Button B1; - Notificationmanager Nmanager; - Notification Notification; - intNotificationid=1; + - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at - Setcontentview (r.layout.notificationlayout); -b1=(Button) Findviewbyid (R.ID.NOTIFICATION_BT1); -B1.setonclicklistener (NewOnclicklistener () { - @Override - Public voidOnClick (View v) { in //sendnotification (); - sendcustomnotification (); to }} + ); - } the * //Send custom Notifications $ Public voidsendcustomnotification () {Panax Notoginseng //1. Get Notificationmanager -Nmanager=(Notificationmanager) Getsystemservice (context.notification_service); the //2. Create Notification +notification =NewNotification ( A R.drawable.folder_open, the"Received file", + System.currenttimemillis () - ); $Remoteviews RV =Newremoteviews (Getpackagename (), r.layout.notificationinterfacelayout); $ Rv.setimageviewresource (r.id.notification_img, r.drawable.savefile); -Rv.settextviewtext (R.id.notification_title, "Lullaby. mp3"); -Rv.setprogressbar (R.id.notification_progressbar, 100, 20,false); thenotification.contentview=RV; - //3. Set the properties, which are displayed after the status bar is expandedWuyiIntent Intent =NewIntent ( This, Toastactivity.class);//turn to other theIntent.setflags (intent.flag_activity_clear_top|intent.flag_activity_new_task); -Pendingintent pintent=pendingintent.getactivity ( This, 0, intent, 0); WuNotification.contentintent=pintent; - //4. Send notification to manager AboutNmanager.notify (notificationid++, notification); $ } - - //Send Notifications - Public voidsendnotification () { A //1. Get Notificationmanager +Nmanager=(Notificationmanager) Getsystemservice (context.notification_service); the //2. Create Notification -notification =NewNotification ( $ R.drawable.folder_open, the"Received file", the System.currenttimemillis () the ); the //Optional Properties -notification.defaults|=Notification.default_sound; inNotification.flags |=notification.flag_insistent; the the About //3. Set the properties, which are displayed after the status bar is expanded theIntent Intent =NewIntent ( This, Toastactivity.class);//turn to other theIntent.setflags (intent.flag_activity_clear_top|intent.flag_activity_new_task); thePendingintent pintent=pendingintent.getactivity ( This, 0, intent, 0); +Notification.setlatesteventinfo ( This, "Receive file", "File already download complete", pintent); - //4. Send Notification to Manager theNmanager.notify (notificationid++, notification);Bayi } the the}
Android development tool Class 16_notificationactivity