安卓(Android) 怎么显示通知(notification)

2019年10月9日22:59:35安卓(Android) 怎么显示通知(notification)已关闭评论 256 892字阅读2分58秒

1.简介

安卓(Android) 怎么显示通知(notification)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Main {
    @SuppressLint("NewApi")
    public static void showNotification(Context context, Intent intent,
            int notifyid, int iconid, String title, String msg) {
        PendingIntent pi = PendingIntent.getActivity(context, 1000, intent,
                0);
        NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentIntent(pi).setSmallIcon(iconid)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle(title).setContentText(msg);

        Notification n = builder.build();
        n.defaults = Notification.DEFAULT_SOUND;
        nm.notify(notifyid, n);
    }
}

 
继续阅读
编程十万个怎么办