Android中通知栏的使用方法

来源:互联网 发布:windows powershell 编辑:程序博客网 时间:2024/05/01 03:56

3.0以上系统中Notification 的setLatestEventInfo已经不推荐使用了,所以用Notification.Builder改造了一下:

显示通知栏

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


public void setNotification() {

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

                    new Intent("com.android.XXX.XXX")
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
            Builder builder = new Notification.Builder(this);
            builder.setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.XXX)
                    .setWhen(System.currentTimeMillis()).setAutoCancel(false)
                    .setContentTitle(statusContentTitle).setContentText(statusContentContext);
            startForeground(XXX, builder.getNotification());

nm.notify(1, notification);

                }

隐藏通知栏
public void closeNotification() {
if (nm != null) {
nm.cancel(1);
}
}

0 0
原创粉丝点击