安卓(Android) 怎么生成通知(Notification)

2019年10月7日22:00:53安卓(Android) 怎么生成通知(Notification)已关闭评论 468 961字阅读3分12秒

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 generateNotification(Context context,
            String message, Intent notificationIntent) {

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        String title = context.getString(R.string.app_name);
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(message).setContentText(title)
                .setContentIntent(intent)
                .setSmallIcon(R.drawable.ic_launcher);

        Notification notification = builder.build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
}

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