Notification 通知

使用通知
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 使用pendingIntent 让通知可点击
        Intent intent = new Intent(this, Notification.class);
        PendingIntent pi = PendingIntent.getActivities(this, 0, new Intent[] { intent }, 0);

        // 1创建NotificationManager 管理通知
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // 2创建notification 对象
        Notification notification = new Notification.Builder(this)
            .setContentTitle("标题")
            .setContentText("内容")
            .setWhen(System.currentTimeMillis()) //通知被创建的时间"
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
            .setContentIntent(pi)  // 把pendingIntent 的实例传入
            .setAutoCancel(true) // 通知点击查看后自动关闭消息提示
            .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/播放音频文件")))
            .setVibrate(new long[] { 0, 1000, 1000, 1000 }) // 通知时候震动和静止时长 需要在manifest里声明 permission.VIBRATE权限
            .setLights(Color.GREEN, 1000, 1000) // 呼吸灯显示
            .setDefaults(NotificationCompat.DEFAULT_ALL) // 使用手机默认的声音 震动 呼吸灯提示
            .setStyle(new Notification.BigTextStyle().bigText("设置长文本数据显示"))
            .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))) // 设置大图片显示
           .setPriority(Notification.PRIORITY_MAX) // 设置通知的重要程度
            .build();
        // 3显示通知  每个通知都有唯一的id,对象
        manager.notify(1, notification);
        //manager.cancel(1); 另一种方式关闭消息通知 1表示通知的id
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值