Android Notification usage (Lock Notification bar), android Notification bar

Source: Internet
Author: User

Android Notification usage (Lock Notification bar), android Notification bar

Recently, I have been researching android and using it. In this case, the program Notification must be resident in the Notification bar and cannot be cleared (just like android QQ. After research and implementation of its function, the use of Notification is summarized as follows:

To use Notification, You need to import three classes.

123 import android.app.PendingIntent;import android.app.NotificationManager;import android.app.Notification;

Code example and description

123456789101112131415161718 NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);              Notification n = new Notification(R.drawable.chat,"Hello,there!", System.currentTimeMillis());             n.flags = Notification.FLAG_AUTO_CANCEL;                Intent i = new Intent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          //PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(        arg0.getContext(),        R.string.app_name,        i,        PendingIntent.FLAG_UPDATE_CURRENT);                 n.setLatestEventInfo(        arg0.getContext(),        "Hello,there!",        "Hello,there,I'm john.",        contentIntent);nm.notify(R.string.app_name, n);

The following code is analyzed in sequence:

1 NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Create NotificationManager. The created nm object is responsible for sending and canceling notifications.

12 Notification n = new Notification(R.drawable.chat,"Hello,there!", System.currentTimeMillis());             n.flags = Notification.FLAG_ONGOING_EVENT; 

Create Notification. The parameters are the resource id of the icon in sequence. The scroll information and time displayed on the status bar are displayed. The created n object is used to describe the information that appears in the system notification bar. Then we will see the Intent set on the n object and click this notification.

1 n.flags = Notification.FLAG_AUTO_CANCEL;

Set n. flags to Notification. FLAG_AUTO_CANCEL. This flag indicates that the Notification can be cleared after you Click Clear.

12 Intent i = new Intent(arg0.getContext(), NotificationShow.class);i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

Creates an Intent, which enables the Intent to be sent after the user clicks the notification.

Note that if you want to start an Activity with this Intent, you must set the Intent. FLAG_ACTIVITY_NEW_TASK tag.

Intent. FLAG_ACTIVITY_CLEAR_TOP

Intent. FLAG_ACTIVITY_NEW_TASK: The system checks whether the Task of the Activity to be started exists in all created tasks. If yes, the Activity is created on the Task, if not, create a Task with the Activity attribute and create the Activity on the Task. For more information, see "(reprinted) Affinities and tasks under Android"

123456 //PendingIntentPendingIntent contentIntent = PendingIntent.getActivity(        arg0.getContext(),        R.string.app_name,        i,        PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent is the Intent package. Here is the description of the startup Intent. PendingIntent returned by PendingIntent. getActivity indicates that the Intent in this PendingIntent instance is the Intent used to start the Activity. The parameters of PendingIntent. getActivity are Context, and the request code of the sender (0 can be entered) is used for the Intent sent by the system.

PendingIntent. FLAG_UPDATE_CURRENT indicates that if the PendingIntent of the description already exists, the Extra data of the existing PendingIntent is changed to the Extra data of the new PendingIntent.

Here we will briefly introduce the differences between Intent and PendingIntent:

Intent: indicates the intention, that is, to tell the system what I want to do, and then the system will do the corresponding thing according to this Intent. For example, startActivity is equivalent to sending a message, while Intent is the content of the message.

PendingIntent: Wrap Intent. Intent is our intention to start a job directly using startActivity, startService or sendBroadcast. In some cases, we cannot directly call startActivity, startServide, or sendBroadcast, but send Intent only when the program or system reaches a certain condition. For example, after you click Notification, the system sends an Intent of the Activity. Therefore, if we do not need to tell the system in some way, the system does not know whether to use startActivity, startService or sendBroadcast to start Intent (of course there are other "descriptions "), therefore, PendingIntent is required here.

12345 n.setLatestEventInfo(        arg0.getContext(),        "Hello,there!",        "Hello,there,I'm john.",        contentIntent);

Set the information displayed in the notification drop-down box. The parameters are Context, title, content, and PendingIntent.

1 nm.notify(R.string.app_name, n);

Start Notification. The parameters are: id value of Notification in your program (used to distinguish different policycation values in the same program, if there is only one Notification in the program, you can enter anything here, but the type must be int), the Notification to be notified.

How can I make my Notification appear in the "running" column like Android QQ?

In fact, it is very simple. You only need to set Notification. flags = Notification. FLAG_ONGOING_EVENT.

How to change the layout of Notification in the "running" column

Create RemoteViews and assign them to Notification. contentView, and then assign PendingIntent to Notification. contentIntent, for example:

12345678910111213 PendingIntent contentIntent = PendingIntent.getActivity(    arg0.getContext(),    R.string.app_name,    i,    PendingIntent.FLAG_UPDATE_CURRENT);             RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);rv.setImageViewResource(R.id.image, R.drawable.chat);rv.setTextViewText(R.id.text,"Hello,there,I'm john.");n.contentView = rv;n.contentIntent = contentIntent; nm.notify(R.string.app_name, n);

NOTE: If contentView is used, do not use Notification. setLatestEventInfo. If setLatestEventInfo is assigned to Notification. after the contentView code, the contentView effect will be overwritten and the result of setLatestEventInfo will be displayed. If setLatestEventInfo is in Notification. before contentView code, Notification is displayed. the contentView effect, that is, whether you want to set the custom Effect of setLatestEventInfo or contentView, ensure that there is always only one set code, because when the last sentence is bound, the code for setting contentView or setLatestEventInfo is completely unnecessary.


How to Set Notification content and icons in the android status bar to drop-down

Use the setLatestEventInfo method with Notification. FLAG _ ***

Android: what is the use of Notification when? Not found after the test

System. currentTimeMillis ();
Get the current number of milliseconds
Can be used to test the program running time

The time is calculated when data is read!
Interval of the prompt in the notification bar = s

The three parameters in the notification bar, the icon content, and the occurrence time.
Notification (icon, tickerText, when );
If there is new content in the same notification bar, the update will be determined based on the time.

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.