zoukankan      html  css  js  c++  java
  • Android8以上 显示通知栏简单实现

      private void showNotification() {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //点击通知本身会显示ResultActivity
            Intent resultIntent = new Intent(this, MainActivity.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            this,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
                mNotificationManager.createNotificationChannel(mChannel);
                notification = new Notification.Builder(this)
                        .setChannelId(CHANNEL_ID)
                        .setContentTitle("活动")
                        .setContentIntent(resultPendingIntent)
                        .setContentText("您有一项新活动")
                        .setSmallIcon(R.mipmap.ic_launcher).build();
            } else {
                mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //构造Builder对象
                builder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("我是标题")
                        .setContentText("我是内容")
                        .setDefaults(Notification.DEFAULT_ALL)//全部
    //                    .setDefaults(Notification.DEFAULT_LIGHTS)//闪光灯
    //                    .setDefaults(Notification.DEFAULT_VIBRATE)//震动
    //                    .setDefaults(Notification.DEFAULT_SOUND)//声音
                ; // requires VIBRATE permission  消息提醒设置
                notification = builder.build();
                builder.setContentIntent(resultPendingIntent);
            }
            mNotificationManager.notify(1, notification);
    
        }
  • 相关阅读:
    a标签 不触发 目标链接
    java Byte[] to String(hex)
    error C2664: 'BOOL (PCERT_SELECT_STRUCT_A)' : cannot convert parameter 1 from 'CERT_SELECT_STRUCT *' to 'PCERT_SELECT_STRUCT_A'
    java jni c++ 例子
    Java.io.DataInputStream.readInt()
    sso demo mysql ( cas )
    sso demo 取消https (cas)
    poj 1422 Air Raid (二分匹配)
    poj 1274 The Perfect Stall (二分匹配)
    hdu 1392 Surround the Trees (凸包)
  • 原文地址:https://www.cnblogs.com/loaderman/p/12672066.html
Copyright © 2011-2022 走看看