NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(MainActivity.this); Resources res = MainActivity.this.getResources(); //显示在最上边的小图 builder.setSmallIcon(R.drawable.bird); //点开后的大图 builder.setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.dolphin)); //短暂显示的提示 builder.setTicker("This is Ticker"); //通知触发时间:即时 builder.setWhen(System.currentTimeMillis()); //点击通知栏后自动消失 builder.setAutoCancel(true); //标题 builder.setContentTitle("This is the content title"); //内容 builder.setContentText("This is the content text"); //添加点击事件 Intent intent = new Intent(MainActivity.this,AnotherActivity.class); PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pi); //获得Notification Notification notification = builder.build(); //默认声音 notification.defaults = Notification.DEFAULT_SOUND; //自定义声音 //Uri soundUri = Uri.fromFile(new File("/system/media/audio/notifications/Cleverer.ogg")); //notification.sound = soundUri; //自定义震动,数组下表奇数为震动时间(毫秒),偶数为停止时间 long[] vibrates = {0,500,1000,500,1000,500}; notification.vibrate = vibrates; //灯光颜色,但是华为手机测试灯不会亮 notification.ledARGB = Color.GREEN; //灯光闪烁时间(毫秒) notification.ledOnMS = 1000; notification.ledOffMS = 1000; //显示灯光 notification.flags = Notification.FLAG_SHOW_LIGHTS; manager.notify(1,notification);
安卓Notification(更新API)
最新推荐文章于 2024-07-15 10:34:46 发布