王老师教学----迷你小通知
一 ·普通消息通知
效果展示
代码展示
private void initCommon() {
btnCommonMessage = (Button) findViewById(R.id.btn_common_message);
//开启监听
btnCommonMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//manage发送
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//开启一个manager
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图片
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加头
builder.setContentTitle("我是一个头");
//添加一个内容
builder.setContentText("我是一个内容");
//开始展示内容
manager.notify(1,builder.build());
}
});
}
二 ·弹出加点击跳转通知
效果展示
点击可以跳转
代码展示
private void initCommon() {
btnCommonMessage = (Button) findViewById(R.id.btn_common_message);
//开启监听
btnCommonMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//manage发送
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//开启一个manager
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图片
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加头
builder.setContentTitle("我是一个头");
//添加一个内容
builder.setContentText("我是一个内容");
//开始展示内容
manager.notify(1,builder.build());
}
});
}
大图消息通知
效果展示
代码展示
private void initTan() {
btnTanMessage = (Button) findViewById(R.id.btn_tan_message);
//开启监听
btnTanMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建构造者
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加头
builder.setContentTitle("这是一个不一样的头");
//添加内容
builder.setContentText("这是一个弹出消息");
//跳转完消失
builder.setAutoCancel(true);
//创建跳转方向
Intent intent = new Intent(MainActivity.this, MainActivity.class);
//添加跳转条件 远程监控
PendingIntent activity = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//添加跳转
builder.setContentIntent(activity);
//添加权限
builder.setPriority(Notification.PRIORITY_MAX);
//添加
builder.setDefaults(Notification.DEFAULT_ALL);
//发送消息
manager.notify(2,builder.build());
}
});
}
列表消息通知
效果展示
代码展示
private void intiImage() {
btnImageMessage = (Button) findViewById(R.id.btn_image_message);
btnImageMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//准备发送
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建构造者
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加大图
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
Notification.BigPictureStyle bigPictureStyle1 = bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle1);
//添加点击消失
builder.setAutoCancel(true);
//创建跳转方向
Intent intent = new Intent(MainActivity.this, MainActivity.class);
//添加跳转方向
PendingIntent activity = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(activity);
//发送
manager.notify(3,builder.build());
}
});
}
自定义消息通知
效果展示
代码展示
private void initmy() {
btnMyMessage = (Button) findViewById(R.id.btn_my_message);
//开始监听
btnMyMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//准备manage
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建构造者
Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加布局
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.mylayout);
remoteViews.setImageViewResource(R.id.iv,R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.tv,"我是杨小宝的爸爸");
//添加
builder.setCustomContentView(remoteViews);
//设置添加消失
builder.setAutoCancel(true);
//创建跳转
Intent intent = new Intent(MainActivity.this, MainActivity.class);
//创建跳转方向
PendingIntent activity = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//跳转
builder.setContentIntent(activity);
manager.notify(4,builder.build());
}
});
}
进度消息通知
效果展示
代码展示
private void initpro() {
btnProMessage = (Button) findViewById(R.id.btn_pro_message);
btnProMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开启准备
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建一个构造者
final Notification.Builder builder = new Notification.Builder(MainActivity.this);
//添加小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//添加标题
builder.setContentTitle("下载");
//开启计时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
builder.setContentText("正在下载."+progress);
builder.setProgress(100,progress,false);
progress += 10;
manager.notify(5,builder.build());
if (progress == 100){
builder.setContentText("正在安装");
builder.setProgress(0,0,true);
manager.notify(5,builder.build());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.cancel(5);
timer.cancel();
}
}
},0,1000);
manager.notify(5,builder.build());
}
});
}
消息分组
效果展示
代码展示
private void initgroup() {
btnGroupMessage = (Button) findViewById(R.id.btn_group_message);
btnGroupMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开启
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//开启构造者
Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
Notification.Builder builder2 = new Notification.Builder(MainActivity.this);
Notification.Builder builder3 = new Notification.Builder(MainActivity.this);
//消息相同可以分在一个组
builder1.setGroup("1");
builder1.setSmallIcon(R.mipmap.ic_launcher);
//设置消息隐藏
builder1.setGroupSummary(true);
builder1.setContentTitle("1111");
builder1.setContentText("2222");
//消息相同可以分在一个组
builder2.setGroup("1");
builder2.setSmallIcon(R.mipmap.ic_launcher);
//设置消息隐藏
builder2.setGroupSummary(true);
builder2.setContentTitle("3333");
builder2.setContentText("4444");
//消息相同可以分在一个组
builder3.setGroup("1");
builder3.setSmallIcon(R.mipmap.ic_launcher);
//设置消息隐藏
builder3.setGroupSummary(true);
builder3.setContentTitle("5555");
builder3.setContentText("6666");
manager.notify(1,builder1.build());
manager.notify(2,builder2.build());
manager.notify(3,builder3.build());
}
});
}
popwindow弹窗
代码展示
设置时间和清晰度
//出框时
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000">
<translate android:fromYDelta="0" android:toYDelta="300"></translate>
<alpha android:fromAlpha="1" android:toAlpha="0.1"></alpha>
</set>
//入框时
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000">
<translate android:fromYDelta="-300" android:toYDelta="0"></translate>
<alpha android:fromAlpha="0.1" android:toAlpha="1"></alpha>
</set>
//合并使用 这是一个风格
<style name="pop_my">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
//java代码
public class MyPopActivity extends AppCompatActivity {
private ImageView add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除自带的头文件
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_my_pop);
add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_pop();
}
});
}
private void show_pop() {
PopupWindow popupWindow = new PopupWindow(MyPopActivity.this);
View inflate = LayoutInflater.from(MyPopActivity.this).inflate(R.layout.pop_layout_my, null);
popupWindow.setContentView(inflate);
//设置pop大小
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//设置点外面退出
popupWindow.setOutsideTouchable(true);
//设置风格
popupWindow.setAnimationStyle(R.style.pop_my);
//设置场外透明度
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 0.5f;
getWindow().setAttributes(attributes);
//隐藏是变回透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha = 1f;
getWindow().setAttributes(attributes);
}
});
View inflate1 = LayoutInflater.from(MyPopActivity.this).inflate(R.layout.activity_my_pop, null);
//展示
popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);
}
private void bottom() {
//实例化对象
PopupWindow popupWindow = new PopupWindow(MyPopActivity.this);
//设置属性
View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout_my, null);
//设置布局
popupWindow.setContentView(inflate);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(600);
//设置点击外部取消
popupWindow.setOutsideTouchable(true);
//展示
popupWindow.showAsDropDown(add,-100,-100);
}
}