圆形头像
Glide.with(this).load(R.mipmap.ic_launcher).apply(new RequestOptions().circleCrop()).into(yuan);
1.双击退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ){
if(System.currentTimeMillis() - exitTime > 2000){
Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
return true;
}else{
finish();
}
}
return super.onKeyDown(keyCode, event);
}
2.自定义通知
package com.example.day003;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import java.math.RoundingMode;
public class NotificationActivity extends AppCompatActivity {
private Button sendMessageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
sendMessageId = findViewById(R.id.send_message_id);
sendMessageId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userNotification();
}
});
}
private void userNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentText("内容");
builder.setContentTitle("头部");
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);
remoteViews.setTextViewText(R.id.rm_text_id,"十七岁的老冯");
remoteViews.setImageViewResource(R.id.rm_image_id,R.mipmap.ic_launcher);
builder.setCustomContentView(remoteViews)
Notification build = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(2,build);
}
3.最简单的通知
package com.example.day003;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NotificationActivity extends AppCompatActivity {
private Button sendMessageId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
sendMessageId = findViewById(R.id.send_message_id);
sendMessageId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotification();
}
});
}
private void sendNotification() {
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("我是标题");
builder.setContentText("我是内容");
Notification build = builder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,build);
}
}
4.进度条通知
private void progress_notification() {
final NotificationManager manager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("我是标题");
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
int progress;
@Override
public void run() {
builder.setContentText("正在下载,当前进度"+progress);
builder.setProgress(100,progress,false);
progress+=10;
manager.notify(6,builder.build());
if(progress==100){
builder.setContentText("正在安装");
builder.setProgress(0,0,true);
manager.notify(6,builder.build());
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
e.printStackTrace();
}
manager.cancel(6);
timer.cancel();
}
}
}, 0, 1000);
}
5.锁屏通知
.setVisibility(VISIBILITY_PUBLIC)
.build();
6.大屏通知和列表通知
private void notificationStyle() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("列表通知");
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("李白");
inboxStyle.addLine("猴子");
inboxStyle.addLine("露娜");
builder.setStyle(inboxStyle);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent intent1 = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setFullScreenIntent(intent1, true);
builder.setContentIntent(intent1);
manager.notify(9, builder.build());
}
7.设置通知效果
builder.setDefaults(Notification.DEFAULT_ALL);
Notification.DEFAULT_VIBRATE
Notification.DEFAULT_SOUND
Notification.DEFAULT_LIGHTS
Notification.DEFAULT_ALL
各个属性进阶版
private void showNotifyOnlyText() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(mLargeIcon)
.setContentTitle("我是只有文字效果的通知")
.setContentText("我没有铃声、震动、呼吸灯,但我就是一个通知");
mManager.notify(1, builder.build());
}
private void showNotifyWithRing() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有铃声效果的通知")
.setContentText("美妙么?安静听~")
.setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
mManager.notify(2, builder.build());
}
private void showNotifyWithVibrate() {
long[] vibrate = new long[]{0, 500, 1000, 1500};
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是伴有震动效果的通知")
.setContentText("颤抖吧,凡人~")
.setVibrate(vibrate);
mManager.notify(3, builder.build());
}
private void showNotifyWithLights() {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是带有呼吸灯效果的通知")
.setContentText("一闪一闪亮晶晶~")
.setLights(0xFF0000, 3000, 3000);
Notification notify = builder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mManager.notify(4, builder.build());
}
}, 10000);
}
private void showNotifyWithMixed() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
.setContentText("我是最棒的~")
.setDefaults(Notification.DEFAULT_ALL);
mManager.notify(5, builder.build());
}
private void showInsistentNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("我是一个死循环,除非你取消或者响应")
.setContentText("啦啦啦~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_INSISTENT;
mManager.notify(6, notify);
}
private void showAlertOnceNotify() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("仔细看,我就执行一遍")
.setContentText("好了,已经一遍了~")
.setDefaults(Notification.DEFAULT_ALL);
Notification notify = builder.build();
notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mManager.notify(7, notify);
}
private void clearNotify() {
mManager.cancelAll();
}