Android Basics (13)

Source: Internet
Author: User

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
    1. Build a notification

Android Basics (13)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.