You have a special delivery.
- You have a special delivery.
- Brief introduction
- Body
- Extended Reading
Target group: Android Beginner without foundation
Knowledge Points: Using the notification bar based on the V4 package
Goal: Show a notification on the notification bar
Brief introduction
Notificationcompat.builder's related notes
Pendingintent's related notes
Notificationmanager's related notes
Body
1. First we need a Notificationcompat.builder object instance, a simple initialization process is as follows
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("大家好") .setContentText("这里是一个通知栏样式");
This class comes from sdk\extras\android\support\v4
The three methods that were called after instantiation set the icon, title, and body contents separately
2. Then we need to set the intention to click on this notification bar to execute
Intent resultIntent = new Intent(this, HelloActivity.class); resultIntent.putExtra("editTxt""来自于通知栏"); PendingIntent resultPendingIntent = PendingIntent.getActivity0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent);
- When the notification bar is clicked, it jumps to the Helloactivity page and passes a string value named Edittxt
3. Finally, use the Notificationmanager class to display our defined Mbuilder objects on the notification bar
// 为本次通知指定一个ID int001; // 得到NotificationManager的一个系统服务单例 NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 调用mBuilder的创建方法并显示 mNotifyMgr.notify(mNotificationId, mBuilder.build());
- Each ID corresponds to a notification object, and when the ID is the same, the new notification object replaces the old
4. Re-build your project and run it on the emulator, if you can see a notification bar appears, and click to jump to a new page, then congratulations, this chapter completed
Extended Reading
- Build a notification
Android Basics (13)