如何在Android的多状态栏通知

编程入门 行业动态 更新时间:2024-10-24 14:25:56
本文介绍了如何在Android的多状态栏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要创建多个状态栏通知。当我下拉状态栏,多个通知图标应显示为列表。每个通知图标应该显示单独的数据显示在接下来的page.How我能做到这一点?

I need to create multiple statusbar notifications. When i pull down the statusbar, multiple notification icons should be displayed as a list. Each notification icon should show separate data to display on next page.How could i do this?

我的code:

public class SimpleNotification extends Activity { private NotificationManager mNotificationManager; private int SIMPLE_NOTFICATION_ID; String str="Hai"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis()); Button start = (Button)findViewById(R.id.notifyButton); Button cancel = (Button)findViewById(R.id.cancelButton); start.setOnClickListener(new OnClickListener() { public void onClick(View v) { Context context = getApplicationContext(); CharSequence contentTitle = "Notification Details..."; CharSequence contentText = "Browse Android Official Site by clicking me"; Intent notifyIntent = new Intent(SimpleNotification.this, sub.class); Bundle bundle = new Bundle(); bundle.putString("welcome",str); notifyIntent.putExtras(bundle); PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); } });

在这里,我做了一个通知,但我需要创建的每个通知应该显示的每个数据的多个通知。

Here I did for one notification, but I need to create multiple notifications that each notification should show each data.

推荐答案

您需要传递一个唯一的ID为每个通知。一旦你点击了通知,您使用的ID将其删除。

You need to pass a unique ID to each notification. Once you have clicked on the notification you use that ID to remove it.

public class SimpleNotification extends Activity { private NotificationManager mNotificationManager; private int SIMPLE_NOTFICATION_ID_A = 0; private int SIMPLE_NOTFICATION_ID_B = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Button start = (Button) findViewById(R.id.start_button); start.setOnClickListener(new OnClickListener() { public void onClick(View v) { // display A displayNotification("Extra for A", "This is A", "Some text for activity A", MyActivityA.class, SIMPLE_NOTFICATION_ID_A); // display B displayNotification("Extra for B", "This is B", "Some text for activity B", MyActivityB.class, SIMPLE_NOTFICATION_ID_B); } }); } private void displayNotification(String extra, String contentTitle, String contentText, Class<?> cls, int id) { Notification notifyDetails = new Notification(R.drawable.icon, "New Alert!", System.currentTimeMillis()); Intent intent = new Intent(this, cls); intent.putExtra("extra", extra); PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_ONE_SHOT); notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent); mNotificationManager.notify(id, notifyDetails); } }

MyActivityA - 中的onCreate()

MyActivityA - in onCreate()

... mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.cancel(SIMPLE_NOTFICATION_ID_A); ...

更多推荐

如何在Android的多状态栏通知

本文发布于:2023-11-27 12:10:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态栏   通知   如何在   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!