6

Tôi là người ăn xin trong lập trình Android, tôi phải làm một CountdownTimer bắt đầu từ một số được chọn bởi người dùng bằng cách sử dụng hai bộ chọn số (một cho giờ và khác cho phút).Đồng hồ đếm ngược Thông báo

Các đặc tính phải có:

-Các CountdownTimer phải làm việc ở chế độ nền và thông báo cho người dùng khi nó đến để 00:00. I Have mã này:

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

}); 
} 

} 

Tôi không biết làm thế nào để làm cho rằng CountdownTimer bắt đầu từ số bởi người sử dụng với hái số lượng và thông báo khi nó được thực hiện lựa chọn.

Vui lòng ai đó giúp tôi. :(

Trả lời

7

TimerTimerTask lớp Sử dụng của Android. Dưới đây tôi giả định rằng bạn biết làm thế nào để hiển thị các thông báo, vì nó xuất hiện bạn làm trong câu hỏi của bạn.

Cùng với hái số của bạn, có một Button quy định tại tĩnh của bạn bố trí như sau:

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

trên tôi giả sử rằng bạn cũng có hái số của bạn trong cùng bố trí

mã Java hoạt động có thể thực hiện như sau:.

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

Cảm ơn bro ... = D nhưng bạn có biết cách giảm thiểu ứng dụng xuống bakground khi nhấn nút .. ??? – jhonand094

+3

Thử 'moveTaskToBack (đúng);' theo [thread này] (http://stackoverflow.com/questions/2041891/sending-activity-to-background-with-out-finishing) – poisondminds

Các vấn đề liên quan