Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Notification to display notifications on the status bar in Android

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Notification to display notifications on the status bar in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Notification to display notifications on the status bar in Android.

(1) call the getSystemService () method to get the NotificationManager service of the system.

(2) create a Notification object and set various properties for it

(3) set event information for Notification object

(4) send Notification notification through the notify () method of the NotificationManager class

The following is an example of how to use Notification to display notifications on the status bar:

Res/layout/main.xml:

This is the click notification jump page main2.xml:

Add the following two permissions to AndroidManifest.xml in, and register ContentActivity in the tag:

MainActivity:

Package com.example.test; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity {public static int NOTIFYID_1=1,NOTIFYID_2=2; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main) / / get notification manager for sending notifications final NotificationManager notificationManager= (NotificationManager) getSystemService (NOTIFICATION_SERVICE); Button button1= (Button) findViewById (R.id.button1) / / get the Show Notification button / / add a click event listener button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {Notification notify=new Notification ()) to the Show Notification button; / / create a Notification object notify.icon=R.drawable.in; notify.tickerText= "Show first Notification"; notify.when=System.currentTimeMillis () / / set sending time (set to current time) notify.defaults=Notification.DEFAULT_ALL;// sets default sound, default vibration and default flash notify.setLatestEventInfo (MainActivity.this, "Untitled", "Progress a little bit every day", null); / / sets event message notificationManager.notify (NOTIFYID_1,notify) / / send notifications through Notification Manager / / add a second notification Notification notify1=new Notification (R.drawable.music, "Show second notification", System.currentTimeMillis ()); the icon disappears after notify1.flags=Notification.FLAG_AUTO_CANCEL;// opens the application Intent intent=new Intent (MainActivity.this,ContentActivity.class) / / set the Intent / / wrapper object for the intention for the jump page. Below is the component object (context, request code, intention object, identifier) PendingIntent pendingIntent=PendingIntent.getActivity (MainActivity.this, 0, intent, 0) activated when the notification is clicked. / / set the content of the notification (context object, title, content, specify where to jump when the notification is clicked, which component is activated) notify1.setLatestEventInfo (MainActivity.this, "notification", "view details", pendingIntent); notificationManager.notify (NOTIFYID_2,notify); / / send notification} through the notification manager) Button button2= (Button) findViewById (R.id.button2); / get the Delete Notification button / / add a click event listener button2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {notificationManager.cancel (NOTIFYID_1) to the display Notification button; / / clear the notification notificationManager.cancelAll whose ID number is constant notification () / / clear all notifications}});}} at this point, I believe you have a better understanding of "how to use Notification to display notifications on the status bar in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 254

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report