popupWindow的使用
//1.創(chuàng)建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() {//按鈕事件監(jiān)聽處理
@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.發(fā)送通知
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.發(fā)送通知
notificationManager.notify(100,notification);