简体   繁体   English

如何在按钮单击时创建具有特定时间间隔的重复通知以及在 android 中单击停止通知

[英]how to create on button click start repeated notifications with specific time interval and on click stop notifications in android

creating a water remainder app and stuck on this for two days.创建一个水剩余应用程序并坚持了两天。 there are 3 buttons to 15min, 30min, 60min user can click any one of this button then by clicking on Start Remainder button application will generate notification reminders ("Time to Drink Water") for every 15,30 or 60 min based on selected button.有 3 个按钮分别为 15 分钟、30 分钟、60 分钟,用户可以单击此按钮中的任何一个,然后单击“开始剩余”按钮,应用程序将根据所选按钮每 15、30 或 60 分钟生成一次通知提醒(“喝水时间”) . On click Stop Reminder button all reminders will end.单击“停止提醒”按钮,所有提醒都将结束。 am new to android trying my best and i hope stackoverflow community will help.我是 android 的新手,我会尽力而为,希望 stackoverflow 社区能提供帮助。

waterRemainder.java waterRemainder.java

public class waterRemainder extends AppCompatActivity implements View.OnClickListener {

    Button min15, min30, min60, rstart, rstop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water_remainder);

        min15 = (Button) findViewById(R.id.min15);
        min15.setOnClickListener(this);

        min30 = (Button) findViewById(R.id.min30);
        min30.setOnClickListener(this);

        min60 = (Button) findViewById(R.id.min60);
        min60.setOnClickListener(this);

        rstart = (Button) findViewById(R.id.rstart);
        rstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartRemainder();
            }
        });

        rstop = (Button) findViewById(R.id.rstop);
        rstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StopRemainder();
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.min15:
                SetRemainder(15);
            case R.id.min30:
                SetRemainder(30);
            case R.id.min60:
                SetRemainder(60);
        }

    }

    public void SetRemainder(int minutes){

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE,5);

    }

    public void StartRemainder(){

        NotificationCompat.Builder builder = new NotificationCompat.Builder(waterRemainder.this,"My Notification")
                .setContentTitle("New Notification")
                .setContentText("its working")
                .setSmallIcon(R.drawable.ic_message)
                .setAutoCancel(true);

    }

    public void StopRemainder(){

    }
}

activity_water_remainder.xml activity_water_remainder.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_header_selector"
    tools:context=".waterRemainder">
    
    <Button
        android:id="@+id/min15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="15 Min"
        android:layout_above="@+id/min30"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp"/>

    <Button
        android:id="@+id/min30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30 Min"
        android:layout_above="@+id/min60"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp"/>
    
    <Button
        android:id="@+id/min60"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="60 Min"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/rstart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="START&#10;REMINDER"
        android:layout_below="@+id/min60"
        android:layout_toEndOf="@+id/min60"
        android:layout_marginStart="20dp"
        android:layout_marginTop="25dp"/>

    <Button
        android:id="@+id/rstop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="STOP&#10;REMINDER"
        android:layout_below="@+id/min60"
        android:layout_toStartOf="@+id/min60"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="25dp"/>
    
</RelativeLayout>

In SetRemainder function do the following:在 SetRemainder function 中执行以下操作:

Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
            //here you can show notification method 
        }
    }, delay, period);
}

where delay is the amount of time in milliseconds before first execution.. In your case set it to 0 and period is the amount of time in milliseconds between subsequent executions which in your case will be the 'minutes' variable.其中 delay 是第一次执行之前的时间量(以毫秒为单位)。在您的情况下,将其设置为 0,period 是后续执行之间的时间量(以毫秒为单位),在您的情况下,这将是“分钟”变量。

And write timer.cancel();并编写 timer.cancel(); in the stop method to cancel the timer.在停止方法中取消定时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM