목차
mBuilder.build() 가 Notification()(NotificationCompat) object 를 return 한다. notification 에서 사용할 data 를 만들어서 notificationManager 에게 준다고 보면 되겠다.
NotificationTestActivity.java
package com.example.com.mugallery; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.app.TaskStackBuilder; import android.util.Log; import android.widget.RemoteViews; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class NotificationTestActivity extends Activity { public final static String TAG = "Demo"; private final static int mId = 23; // private Builder mNotifyBuilder; private NotificationManager mNotificationManager; private NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this); /** * * <pre> +------------------------------------------+ | +--------------+---++------------------+ | +---------------------+ | | || | | | | | | || smallIcon | | | NotificationManager | | | || | | | | | | |+------------------+ | +-------+-------------+ | | Builder |+------------------+ | | | | | || | | | | | | || ContentTitle | |+------->| | | | || | | |notify() | | |+------------------+ | | | | | |+------------------+ | | | | | || | | | | | | || ContentText | | +----+--+ | | || | | | | | |+------------------+ | | | | |+------------------+ | | | | || | | v | | || SetContentIntent | | +---------++------------+ | | || | | | || | | +------------------++------------------+ | | queue || | +------------------------------------------+ +---------+| | | service | | | | | | | | | | | +------------+ </pre> */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mNotifyBuilder.setSmallIcon(R.drawable.gallery_photo_1) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, NotificationTestActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(NotificationTestActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mNotifyBuilder.setContentIntent(resultPendingIntent); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(mId, mNotifyBuilder.build()); Thread t = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(3000); doUpdate(); Thread.sleep(3000); doDownload(); Thread.sleep(3000); cancel(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t.run(); } public void doUpdate(){ // Sets an ID for the notification, so it can be updated int notifyID = mId; mNotifyBuilder .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.drawable.gallery_photo_1); int numMessages = 0; // Start of a loop that processes data and then notifies the user mNotifyBuilder.setContentText("added message") .setNumber(++numMessages); // Because the ID remains unchanged, the existing notification is // updated. mNotificationManager.notify( notifyID, mNotifyBuilder.build()); } public void doDownload(){ int notifyID = mId; int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr+=5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state mNotifyBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. mNotificationManager.notify(notifyID, mNotifyBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep(5*1000); } catch (InterruptedException e) { Log.d(TAG, "sleep failure"); } } } public void cancel(){ mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Sets an ID for the notification, so it can be updated mNotificationManager.cancel(mId); } }
Screenshots
References
- Displaying Progress in a Notification, Notifications, Android API Doc.
- Creating a simple notification, Notifications, Android API Doc.
- Updating notifications, Notifications, Android API Doc.
댓글 없음:
댓글 쓰기