android - 如何在android中创建多个状态栏通知

标签 android android-notifications

我需要创建多个状态栏通知。当我拉下状态栏时,应将多个通知图标显示为列表。每个通知图标应显示单独的数据以显示在下一页上。我该怎么做?

我的代码:

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);
        }
    });

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

最佳答案

您需要为每个通知传递一个唯一的 ID。单击通知后,您可以使用该 ID 将其删除。

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() 中

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

关于android - 如何在android中创建多个状态栏通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6224178/

相关文章:

Android 构建错误 - Ubuntu 和 TaintDroid

android - 天文台字体在android中没有改变

android - BigPictureStyle 通知在小米 MIUI-8 中无法正确显示

android - 如何获取Android中所有应用程序收到的通知数

android - Nullpointerexception 仅在已签名的 APK 中

java - 如何解决这个 GPS 问题?

android - 构建自定义 Activity 堆栈

android - 无法将 Activity 作为方法中的参数传递

android - 为各种 Android 操作系统版本设置推送通知图标

android - 如何使用通过通知传递的 Intent 更新 Activity