简体   繁体   English

倒数计时器通知

[英]Countdown Timer Notification

I'm begginer in Android programming, I have to do a CountdownTimer that starts from a number selected by the user using a two number pickers (one for Hours and other for minutes). 我是Android编程的开始,我必须做一个CountdownTimer,它从用户使用两个数字选择器选择的数字开始(一个用于小时,另一个用于分钟)。

The characteristics must be: 特征必须是:

-The CountdownTimer must work in background and notify the user when it arrives to 00:00. - CountdownTimer必须在后台工作,并在到达00:00时通知用户。 I Have this code: 我有这个代码:

package com.android.application;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;

public class MainActivity extends Activity {
NotificationManager nm;
private static final int ID_NOTIFICACION_PERSONAL =1;

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

NumberPicker number1 = (NumberPicker)findViewById(R.id.horas);
NumberPicker number2 = (NumberPicker)findViewById(R.id.minutos);
Button btn_lanzar=(Button)findViewById(R.id.btn_notificacion);
number1.setMaxValue(10);
number1.setMinValue(0);
number2.setMinValue(0);
number2.setMaxValue(59);

int number3=number1.getValue();
int number4=number2.getValue();

nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
btn_lanzar.setOnClickListener(new View.OnClickListener(){
    @Override 
    public void onClick(View v) {
    Notification notification = new Notification(R.drawable.estacionamiento,"Estacionamiento Inteligente",System.currentTimeMillis());    
    PendingIntent intencionpendiente = PendingIntent.getActivity(getApplicationContext(),0, new Intent (getApplicationContext(),Segunda_ventana.class),0);
    notification.setLatestEventInfo(getApplicationContext(), "Estacionamiento Inteligente", "Su Tiempo se ha Terminado", intencionpendiente);
    nm.notify(ID_NOTIFICACION_PERSONAL, notification);
    }

});
}

} 

I don't know how to make that the CountdownTimer begins from the number selected by the user with the number pickers and the notification when it is done. 我不知道如何使CountdownTimer从用户选择的号码开始,带有号码选择器和完成时的通知。

Please someone Help Me. 请有人帮助我。 :( :(

Use Android's Timer and TimerTask classes. 使用Android的TimerTimerTask类。 Below I assume that you know how to display notifications, as it appears you do in your question. 下面我假设你知道如何显示通知,就像你在问题中看到的那样。

Along with your number pickers, have a Button defined in your static layout like so: 与您的号码选择器一起,在静态布局中定义一个Button ,如下所示:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start CountDown"
    android:onClick="startCountdown"
 />

Above I assume that you also have your number pickers in that same layout. 上面我假设你也有相同布局的号码选择器。

Java activity code might be as follows: Java活动代码可能如下:

NumberPicker hourPicker;
NumberPicker minutePicker;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    hourPicker = (NumberPicker)findViewById(R.id.horas);
    minutePicker = (NumberPicker)findViewById(R.id.minutos);
    hourPicker.setMaxValue(10);
    hourPicker.setMinValue(0);
    minutePicker.setMinValue(0);
    minutePicker.setMaxValue(59);
}

public void startCountdown(View v)
{
    //this fires when button was tapped
    showTimerStartedNotification();

    int hours = hourPicker.getValue();
    int mins = minutePicker.getValue();

    int millis = ((hours*60)+mins)*60000; // Need milliseconds to use Timer


    new Timer().schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            //code that runs when timer is done
            showTimerFinishedNotification();
        }
    }, millis);
}

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

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