通知栏Notification的应用

使用通知就要先获取一个 NotificationManage 对象,使用getSystemService(“获取系统的哪一个服务”);,然后在获取赢Notification对象, Notification notification = new NotificationCompat.Builder(MainActivity.this),参数传入上下文。然后在。Builder()方法后面连缀去多方法来创建一个丰富的Notification对象。

然后调用manager.notify(1, notification);方法完成创建。

两个参数,第一是ID保证所有通知的ID是不相同的,第二个Notification对象。

通知震动需要添权限:

<uses-permission android:name="android.permission.VIBRATE"/>

Notification的基本功能都在下面代码

public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Two.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("二")
                        //.setContentText("可以跳转")//短文本
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("dsgrth 问题浩如烟海让他加塔吉克就让他" +
                                " uuj tyy7如图统计如若tu656凧个人挺好看tjy7i而已人个人的体会"))//长文本
                        //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)))//显示长图片
                        .setWhen(System.currentTimeMillis())//显示通知出现的时间
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                        .setContentIntent(pendingIntent)//跳转
//                        .setAutoCancel(true)//跳转后消除通知
                        // .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))//指定铃声声音
                        .setDefaults(NotificationCompat.DEFAULT_ALL)//手机默认铃声
                        .setVibrate(new long[]{0, 1000, 1000, 1000})//震动提示,数组里面0下表代表静音,1下表代表震动,以此类推
                        .setLights(Color.GREEN, 1000, 1000)//LED灯闪烁
                        .setPriority(NotificationCompat.PRIORITY_MAX)//显示内容的重要程度
                        .build();
                manager.notify(1, notification);
            }
原文地址:https://www.cnblogs.com/zhoushenglei/p/7194087.html