Android Tips Summary of new and old version notification

Source: Internet
Author: User
Tags deprecated

Recently developed to use the notification function, but there are several places always hint deprecated, and then find an article to learn the new and old version of the difference.

Notification is a notification that displays a prompt message in the notification bar.

In newer versions (API level > One), some of the methods in the notification class are declared by Android deprecated (deprecated), which is basically the equivalent of completely deprecated, because the class itself is poorly done.

Android Official statement deprecated, there must be a reason for it, although I do not know what it is. But I have mild obsessive-compulsive disorder, people are advised you not to use, then do not always abide by the stuff of n years ago.

Like before, when it comes to tabs, we basically think of tabhost with Activitygroup, but Android later advocates fragment.

More nonsense, or a summary of the use of methods. Follow the steps to create a notification step-by-step, and give the old and new implementation methods.

1. Get Notification Manager

Notificationmanager notemng = (notificationmanager) getsystemservice (Notification_service);

2. Create a new notification, set the status bar display style

private Notification Note;
API 11 or less
Note = new Notification (r.drawable.ico_launcher "text displayed in the status bar at the top of the screen", System.currenttimemillis ());
API 11 and above
Notification.builder Builder = new Notification.builder (nowcontext). Setticker ("text displayed on the status bar at the top of the screen")
. Setsmallicon (R.drawable.ic_laucher);

API 11 and above, the status bar display style and drop-down notification bar display style, can be set together, is implemented by the Notification.builder class, where the builder only calls two methods to set the status bar display style.

3. Set notification flag (non-essential steps)
Flag_ongoing_event indicates that a program is running and the notification is not cleared by the user
Note.flags = notification.flag_ongoing_event;

4, set the trigger event after clicking Notification
With intent, a new activity is launched after clicking Notification
Intent i = new Intent (Nowcontext, Anotheractivity.class);
This flag indicates that if intent is to start the activity at the top of the stack, there is no need to create a new instance
I.setflags (Intent.flag_activity_single_top);
Pendingintent pendingintent = pendingintent.getactivity (Nowcontext, N, I, pendingintent.flag_update_current);

5. Set the style of notification in the notification bar
(1) System default Style
API 11 below:
Note.setlatesteventinfo (Nowcontext, "Take Me to Your Heart", "Micheal Learn to Rock", pendingintent);

API 16 and above, build () method requires API 16 and above

One will API 11 above, one will API16 above, I also want to know how the Android API is designed
Note = builder.setcontentintent (pendingintent). Setcontenttitle ("title"). Setcontenttext ("text"). Build ();

(2) Custom style:

Custom style, which is to have notification displayed as a custom XML layout in the notification bar
It should be noted that the notification custom style only supports the following visual components:
Framelayout, LinearLayout, Relativelayout
TextView, Button, AnalogClock, ImageView, ImageButton, chronometer, ProgressBar

RemoteView view = new RemoteView (Nowactivity.getpackagename (), r.layout.note_layout);
API 11 or less
Note.contentview = view;
Note.contentintent = pendingintent;
API 16 and above, is also the build () method caused by, Khan.
Note = builder.setcontent (view). Setcontentintent (pendingintent). build ();

There is one notable point in this step: Pendingintent is set to the Contentintent value of note, which means that clicking on this notification will trigger the intent.

So what if you just want to trigger a button in the custom layout? For example, to get a music player, the service is responsible for playing music, notification shows the current playback progress and some simple pause button, the previous and Next button, so that the user can not open the interface on the notification button to manipulate music playback.

Let's say you want to trigger this intent with a button with the ID r.id.button1 in the custom layout, you can do the following:

View.setonclickpendingintent (R.id.button1, pendingintent);//Add this sentence after creating the RemoteView instance above

Then note that the pendingintent has been bound to the button, above the Notificatiion instance, set the Contentintent statement to be removed.

6, publish the notice, the first parameter is the ID of the notification
Notemng.notify (ten, note);

At the time of encoding, if your minsdkversion settings are not met, you will be prompted immediately. So you have to consider the minimum version of the situation to be satisfied, depending on the size of the current version of the weight of the next, of course, I hope you push the new version of the use, as far as possible with the new version of Development code development.

If you use the new version of the ANDROID16 version below, you will be prompted failure [install_parse_failed_unexpected_exception] at the time of installation. This is because the setting of the minsdkversion is too large for reasons. If compatible, change to a smaller version number can be installed.

Android Tips Summary of new and old version notification

Related Article

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.