@Android安卓多个timer的控制中止,安卓状态栏弹出提醒
安卓多个timer的控制中止,安卓状态栏弹出提醒
最近在安卓实训,发表一下关于timer和安卓状态栏的使用,
重点是多个timer的判断结束条件和timer的使用
多个Timer的结束设置
timer只能在内部结束,即this.cancel
- 关于timer ,timer是一个定时器;
- 关于timerTask ,timerTask是一个执行方法;
- 关于schedule ,schedule是用来安排时间的
一个参数;
如何去控制timer的结束?
点击按钮btn1时,赋值u=1;
这时在执行btn1里的代码
当按钮btn2的时候
赋值u=2并且运行btn2里的代码
同时,u值被改变了btn1里的判断语句
if(u!=1)this.cancel;
就会执行
这时就会结束掉timer了。
代码块
代码、
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv1;
private TextView tv2;
private TextView tv3;
private Button btn1;
private Button btn2;
private Button btn3;
private int u;
private int x = 0, y = 0, z = 0;
```
private int u;
这个值当按钮点击的时候会赋值,
按钮1按下的时候赋值u=1,
而按钮2的判断值是u!=2,当u!=2时,
就会执行this.cancel();
小提示,重复点击一个按钮会执行n个timer,
这里只设置了根据其他按钮的值来执行停止
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
//监听器
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
//点击事件类
@Override
public void onClick(View v) {
switch (v.getId()) {
//按钮1
case R.id.btn1:
u = 1;//赋值
new Timer().schedule(new TimerTask() {
@Override
public void run() {
x++;
if (u != 1) this.cancel();//跳出定时器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//只在Android O之上需要渠道,这里的第一个参数要和下面的channelId一样
NotificationChannel notificationChannel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
manager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(MainActivity.this, "1")
.setContentTitle("道路状态报警")
.setContentText("btn1," + x)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(1, notification);
}
}, 0, 3000);
break;
//按钮2
case R.id.btn2:
u = 2;//赋值
new Timer().schedule(new TimerTask() {
@Override
public void run() {
y++;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//只在Android O之上需要渠道,这里的第一个参数要和下面的channelId一样
NotificationChannel notificationChannel = new NotificationChannel("1", "name", NotificationManager.IMPORTANCE_HIGH);
//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
manager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(MainActivity.this, "1")
.setContentTitle("道路状态报警")
.setContentText("btn2," + y)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(1, notification);
}
}, 0, 3000);
break;
//按钮3
case R.id.btn3:
u = 3;//赋值
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (u != 3) this.cancel();
z++;
//假如其他按钮按了就会改变值,这个就会cancel掉
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//新建对象
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel
("1", "name", NotificationManager.IMPORTANCE_MAX);
manager.createNotificationChannel(notificationChannel);
}
Notification notification = new NotificationCompat.Builder(MainActivity.this, "1")
.setContentTitle("标题提示")
.setContentText("btn3," + z)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
}
}, 0, 3000);//定时器,延迟0秒,定时每三秒执行一次
break;
}
}
}
CSDN编辑快捷
撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G
文章仅供参考