popupWindow的使用
//1.创建PoP(必须包含:显示View、宽、高)
final PopupWindow popupWindow = new PopupWindow(this);
View inflate = LayoutInflater.from(this).inflate(R.layout.layout_item, null);
popupWindow.setContentView(inflate);//显示的View控件
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);//显示宽度
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//显示高度
popupWindow.setFocusable(true);//是否可以获取焦点
PopupWindow popupWindow2 = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, true);
//5.点击外部可以消失
popupWindow.setBackgroundDrawable(new ColorDrawable());
popupWindow.setOutsideTouchable(true);
//6.设置进出动画
popupWindow.setAnimationStyle(R.style.popWindow);
//2.显示
//popupWindow.showAsDropDown(bB);//显示到相对某个控件的正下方
//popupWindow.showAsDropDown(bB,-100,-200);//显示到某个控件正下方,同时与X/Y的偏移值
popupWindow.showAtLocation(btn,Gravity.RIGHT|Gravity.BOTTOM,0,0);//相对父窗体的对齐方式
//3.pop事件处理
Button btn1 = inflate.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {//按钮事件监听处理
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"弹出",Toast.LENGTH_SHORT).show();
}
});
inflate.findViewById(R.id.ll).setOnClickListener(new View.OnClickListener() {//点击阴影关闭pop
@Override
public void onClick(View v) {
//4.关闭Pop
popupWindow.dismiss();
}
});
NotificationManager的使用
低版本
//1.获取通知管理器类
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//延时意图
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//2.构建通知类
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标
builder.setContentTitle("通知");//标题
builder.setContentText("dadsadasdsadsa");//内容
builder.setContentIntent(pendingIntent);//延时意图 : 点击跳转页面
builder.setAutoCancel(false);//自动消失
builder.setDefaults(Notification.DEFAULT_ALL);//提示效果(震动、呼吸灯、声音提示)
//3.获取通知
Notification notification = builder.build();
//4.发送通知
notificationManager.notify(100,notification);
高版本
//1.获取通知管理器类
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//2.构建通知类
String channelId = "1";
String channelName = "default";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);//开启指示灯,如果设备有的话。
channel.setLightColor(Color.RED);//设置指示灯颜色
channel.setShowBadge(true);//检测是否显示角标
notificationManager.createNotificationChannel(channel);
}
//延时意图
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//2.构建通知类
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标
builder.setContentTitle("通知");//标题
builder.setContentText("dadsadasdsadsa");//内容
builder.setContentIntent(pendingIntent);//延时意图 : 点击跳转页面
builder.setAutoCancel(false);//自动消失
builder.setDefaults(Notification.DEFAULT_ALL);//提示效果(震动、呼吸灯、声音提示)
//3.获取通知
Notification notification = builder.build();
//4.发送通知
notificationManager.notify(100,notification);
网友评论