使用Notification通知

使用Notification的步骤:

在主线程的onCreate()(或其他地方)添加以下代码

  

Intent intent=new Intent(this,ContactsContract.CommonDataKinds.Note.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);  //延迟执行
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(this)
        .setContentTitle("通知标题")
        .setContentText("通知正文")
        .setWhen(System.currentTimeMillis()) //何时发出通知
.setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
        .setContentIntent(pi)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        // .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.big_image)))  //设置通知样式
        //.setSound(Uri.fromFile(new File(xxxx)))  //发出通知的同时放出一段音频
        //.setVibrate(new long[]{0,1000,1000,1000}) //设置振动1s 停止1s 振动1s 要申请权限 VIBRATE
.setPriority(NotificationCompat.PRIORITY_MAX)
        .build();
manager.notify(1, notification);  //参数(通知id ,Notification的实例对象)

猜你喜欢

转载自542255641.iteye.com/blog/2395691