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 customize the status bar notification layout

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

Share

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

This article mainly introduces how to customize the status bar notification layout, the article is very detailed, has a certain reference value, interested friends must read it!

Layout definition custom_notification.xml

Style file styles.xml referenced in the center of the cloth

Code

Package cn.itcast.tabhost;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.graphics.Color;import android.widget.RemoteViews Public class FirstActivity extends Activity {/ / by default clicking the return key (back) will finish all the activity in the current activity / / activity stack and exit the current application @ Override public void onBackPressed () {/ * * according to general logic, when there is only one Activity in the Activity stack When you press the Back key this * then the next time you click the application icon will restart. At present, many applications take the effect such as the Home key, * when you click the back key, the system returns to the desktop, and then click the application icon * directly back to the previous Activity interface, how is this effect achieved? By rewriting the callback function that presses the Back key, it can be converted to the effect of the Home key. * / / instead use intent to launch the HOME desktop Intent home = new Intent (Intent.ACTION_MAIN); home.addCategory (Intent.CATEGORY_HOME); startActivity (home); / / or, Activity actually provides a direct way to achieve this effect. / / move the Task of the current Activity to the background, while preserving the order and status of the activity. MoveTaskToBack (true); / / true indicates that it is valid whether or not the root is valid} / * * when the Activity is working in the background, a notification * / @ Override protected void onStop () {showNotification (); super.onStop () is displayed in the status bar. } / / when the program enters the running interface again, Activity is in onResume state You can remove the program running information of the status bar in the onResume method / * the notification that closes the status bar after starting the Activity * / @ Override protected void onResume () {/ / delete the previously defined notification NotificationManager notificationManager = (NotificationManager) this .getSystemService (NOTIFICATION_SERVICE) NotificationManager.cancel (CUSTOM_VIEW_ID); super.onResume ();} private static final int CUSTOM_VIEW_ID = 1 / / display the program in the status bar to notify private void showNotification () {/ / create a reference to NotificationManager NotificationManager notificationManager = (NotificationManager) this .getSystemService (android.content.Context.NOTIFICATION_SERVICE) / / define various attributes of Notification Notification notification = new Notification (R.drawable.bg_normal, "superGao", System.currentTimeMillis ()); RemoteViews contentView = new RemoteViews (getPackageName (), R.layout.custom_notification); contentView.setImageViewResource (R.id.p_w_picpath, R.drawable.i1) ContentView.setTextViewText (R.id.title, "Custom layout Notification title"); contentView.setTextViewText (R.id.text, "Custom layout Notification content"); / / set click events for view / * contentView.setOnClickPendingIntent (viewId, pendingIntent); * / notification.contentView = contentView Notification.flags | = Notification.FLAG_ONGOING_EVENT; / / put this notification in the "Ongoing" in the notification bar, that is, notification.flags in the "running" group | = Notification.FLAG_NO_CLEAR; / / indicates that the notification will not be cleared after clicking "clear Notification" in the notification bar, and notification.flags is often used with FLAG_ONGOING_EVENT | = Notification.FLAG_SHOW_LIGHTS / / use the LED light notification.defaults = Notification.DEFAULT_LIGHTS; notification.ledARGB = Color.BLUE;//LED lamp color notification.ledOnMS = 5000) / set the event message of the notification / * * CharSequence contentTitle = "superGao"; / / notice bar title CharSequence contentText = "love" / / Notification bar content * / Intent notificationIntent = new Intent (this, FirstActivity.class); / / Activity PendingIntent contentItent = PendingIntent.getActivity (this, 0, notificationIntent, 0) to be redirected after clicking the notification; notification.contentIntent=contentItent; / * notification.setLatestEventInfo (this, contentTitle, contentText, contentItent) * / / pass Notification to NotificationManager notificationManager.notify (CUSTOM_VIEW_ID, notification);}} these are all the contents of the article "how to customize the notification layout of the status bar". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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: 0

*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