day04通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setContentTitle("我是标题");
builder.setContentText("我是内容");
builder.setTicker("我是提示信息");
builder.setContentInfo("我是附加内容");
//设置通知自动消失
builder.setAutoCancel(true);
//设置通知的来电效果 振动 音效 灯光 振动需要加权限
builder.setDefaults(Notification.DEFAULT_ALL);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
//设置跳转 进入到第二个界面
PendingIntent activity = PendingIntent.getActivity(this, 101, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(activity);
manager.notify(1,builder.build());
自定义通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
//必须设置一个小图标
builder.setSmallIcon(R.drawable.qqyinle);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.activity3_main);
remoteViews.setTextViewText(R.id.tv1,"绅士");
builder.setContent(remoteViews);
manager.notify(1,builder.build());
//XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:src="@drawable/qqyinle"
android:layout_width="50dp"
android:layout_height="70dp"/>
<TextView
android:textColor="#000"
android:id="@+id/tv1"
android:text="意外"
android:gravity="center"
android:textSize="10dp"
android:layout_width="200dp"
android:layout_height="20dp"/>
<TextView
android:id="@+id/tv2"
android:textColor="#000"
android:text="薛之谦"
android:gravity="center"
android:textSize="10dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="-200dp"
android:layout_width="200dp"
android:layout_height="20dp"/>
<Button
android:background="@drawable/kuaijin2"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="-130dp"
android:layout_width="20dp"
android:layout_height="20dp"/>
<Button
android:background="@drawable/kuaijin3"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="0dp"
android:layout_width="20dp"
android:layout_height="20dp"/>
<Button
android:background="@drawable/kuaijin"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="0dp"
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
进度条通知
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.qqyinle);
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
int progress =0;
@Override
public void run() {
builder.setProgress(100,progress+=10,false);
manager.notify(3,builder.build());
if (progress==100){
builder.setProgress(100,100,true);
manager.notify(3,builder.build());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//吐司的消息通过handler发送消息
Message message = Message.obtain();
message.what=1000;
message.obj="安装完成";
MainActivity.this.handler.sendMessage(message);
manager.cancel(3);
timer.cancel();
}
}
},0,1000);
//接收消息进行吐司
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1000){
String str = (String)msg.obj;
Toast.makeText(MainActivity.this,str+"",Toast.LENGTH_LONG).show();
}
}
};
按两次返回键推出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
long l = System.currentTimeMillis();
if ((l-pre)<=1000){
pre=l;
finish();
}else {
pre=l;
Toast.makeText(this, "再按一次推出", Toast.LENGTH_SHORT).show();
return false;
}
}
return super.onKeyDown(keyCode, event);
}