一切尽在不言中~
public static final String ACTION_BUTTON_LAST = "INTENT_ACTION_NOTIFY_RECEIVER_LAST";
public static final String ACTION_BUTTON_PLAY = "INTENT_ACTION_NOTIFY_RECEIVER_PLAY";
public static final String ACTION_BUTTON_NEXT = "INTENT_ACTION_NOTIFY_RECEIVER_NEXT";
private NotificationManager mNotificationManager;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播 因为在Activity中销毁广播 效果不在地 还是觉得音乐播放的话 放到Service中,这样,服务销毁再销毁广播
ButtonBroadcastReceiver buttonBroadcastReceiver = new ButtonBroadcastReceiver();
IntentFilter intentFilter1 = new IntentFilter();
intentFilter1.addAction(ACTION_BUTTON_LAST);
intentFilter1.addAction(ACTION_BUTTON_PLAY);
intentFilter1.addAction(ACTION_BUTTON_NEXT);
registerReceiver(buttonBroadcastReceiver, intentFilter1);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false);//是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN);//小红点颜色
channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知
mNotificationManager.createNotificationChannel(channel);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.customlayout);
Intent mainIntent=new Intent(this,MainActivity.class);
PendingIntent tomain=PendingIntent.getActivity(this,0,mainIntent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1").setContent(remoteViews)
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentIntent(tomain)
.setWhen(System.currentTimeMillis());
Notification notifycation = mBuilder.build();
// .setContent(remoteViews)
// .setAutoCancel(true)
// .setSound(null)//禁止静音
// .setVibrate(new long[]{0})//禁止震动
// .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
// .setTicker("正在播放")
// .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
// .setOngoing(true)
// .setSmallIcon(R.mipmap.ic_launcher);
//为自定义布局中的按钮定义点击事件
Intent intentLast = new Intent(ACTION_BUTTON_LAST);
PendingIntent but1 = PendingIntent.getBroadcast(this, 0, intentLast, 0);
remoteViews.setOnClickPendingIntent(R.id.custom_btn1, but1);//点击的id,点击事件
Intent intentPlay = new Intent(ACTION_BUTTON_PLAY);
PendingIntent but2 = PendingIntent.getBroadcast(this, 0, intentPlay, 0);
remoteViews.setOnClickPendingIntent(R.id.custom_btn2, but2);//点击的id,点击事件
Intent intentNext = new Intent(ACTION_BUTTON_NEXT);
PendingIntent but3 = PendingIntent.getBroadcast(this, 0, intentNext, 0);
remoteViews.setOnClickPendingIntent(R.id.custom_btn3, but3);//点击的id,点击事件
//代码设置图片
// remoteViews.setImageViewResource(R.id.custom_btn1, R.drawable.video_le);
// remoteViews.setImageViewResource(R.id.custom_btn2, R.drawable.video_ri);
// remoteViews.setImageViewResource(R.id.custom_btn3, R.drawable.play);
notifycation.contentView = remoteViews;
mNotificationManager.notify(1, notifycation);
}
/**
* 广播监听按钮点击事件
*/
public static class ButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case ACTION_BUTTON_LAST:
break;
case ACTION_BUTTON_PLAY:
break;
case ACTION_BUTTON_NEXT:
break;
default:
break;
}
}
}
}
/**
* //普通
* case R.id.btn_1:
* Notification.Builder builder=new Notification.Builder(this);
* Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
* PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
*
*
* builder.setContentIntent(pendingIntent);
* builder.setSmallIcon(R.mipmap.ic_launcher);
* builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
* builder.setAutoCancel(true);
* builder.setContentTitle("普通通知");
* mNotificationManager.notify(1, builder.build());
* break;
* //折叠
* case R.id.btn_2:
* Notification.Builder builder2=new Notification.Builder(this);
* Intent intent2=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
* PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,intent2,0);
* builder2.setContentIntent(pendingIntent2);
* builder2.setSmallIcon(R.mipmap.ic_launcher);
* builder2.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
* builder2.setAutoCancel(true);
* builder2.setContentTitle("折叠通知");
*
* RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_view);
* Notification notification=builder2.build();
* notification.bigContentView=remoteViews;
* mNotificationManager.notify(1,notification);
* break;
* //悬挂----ANDROID5.0新特性
* case R.id.btn_3:
* Notification.Builder builder3=new Notification.Builder(this);
* Intent intent3=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
* PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,intent3,0);
* builder3.setContentIntent(pendingIntent3);
* builder3.setSmallIcon(R.mipmap.ic_launcher);
* builder3.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
* builder3.setAutoCancel(true);
* builder3.setContentTitle("悬挂通知");
*
* Intent XuanIntent=new Intent();
* XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
* XuanIntent.setClass(this,MainActivity.class);
*
* PendingIntent xuanpengdIntent=PendingIntent.getActivity(this,0,XuanIntent,PendingIntent.FLAG_CANCEL_CURRENT);
* builder3.setFullScreenIntent(xuanpengdIntent,true);
* mNotificationManager.notify(2,builder3.build());
*
* break;
*/
8.0兼容
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
String CHANNEL_ONE_ID = "com.as.apprehendschool";
String CHANNEL_ONE_NAME = "Channel One";
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
} //--------------------------------------------------------新增
notification = new Notification.Builder(this)
.setChannelId(CHANNEL_ONE_ID).setTicker("领悟学堂")
.setSmallIcon(R.drawable.app)
.setContentTitle("正在运行")
.setContentText("")
.getNotification();
notification.flags |= Notification.FLAG_NO_CLEAR;
}else{
notification=new Notification();
}