【问题标题】:Countdown Timer Notification倒数计时器通知
【发布时间】:2015-08-30 08:13:43
【问题描述】:

我是 Android 编程的初学者,我必须做一个 CountdownTimer,它从用户使用两个数字选择器(一个用于小时,另一个用于分钟)选择的数字开始。

特征必须是:

- CountdownTimer 必须在后台工作,并在到达 00:00 时通知用户。 我有这个代码:

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);
    }

});
}

} 

我不知道如何使 CountdownTimer 从用户选择的数字开始,并在完成时发出通知。

请有人帮助我。 :(

【问题讨论】:

    标签: android android-activity android-studio background countdowntimer


    【解决方案1】:

    使用 Android 的 TimerTimerTask 类。下面我假设您知道如何显示通知,就像您在问题中所做的那样。

    与您的号码选择器一起,在您的静态布局中定义 Button,如下所示:

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

    上面我假设你的号码选择器也有相同的布局。

    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);
    }
    

    【讨论】:

    • 谢谢兄弟... =D 但是您知道如何按一个按钮将应用程序最小化到bakground ..???
    • 按照this thread尝试moveTaskToBack(true);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多