6

我是Android編程的初學者,我不得不做一個CountdownTimer,它從用戶選擇的一個數字開始,使用兩位號碼選取器(一個用於小時,另一個用於分鐘)。倒數計時器通知

的特點必須是:

-The 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因與多個選擇器的用戶,並且當它完成該通知選擇的號碼開始。

請別人幫我。 :(

回答

7

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

感謝兄弟...... = D 但你知道如何最小化應用程序到bakground按下按鈕嗎? – jhonand094

+3

嘗試'moveTaskToBack(true);'根據[this thread](http://stackoverflow.com/questions/2041891/sending-activity-to-background-with-out-finishing) – poisondminds