程序准备实现的功能:加载时自动进行倒计时
目前的问题:倒计时程序不响应(单独对倒计时程序进行编译,倒计时正常)。
代码见下,各位大大请帮忙分析下,是否是handler出的问题还是因为备注里标明的由于mSleeptime获取不到值为0的原因,困惑了。如果是handler的问题该如何解决呢?
谢谢!
[备注:
- Java code
1,private void GetSharedPreferences() { SharedPreferences settings = getSharedPreferences(SETTING_Infos, 0); mBootNum=settings.getInt(BootNum,1); mSleepTime=settings.getInt(SleepTime,1); mSleepTime是能获取到值得。 2,private String getCountdownString(){ int hour=00; int minute=00; int second = mSleepTime; return String.format("%02d:%02d:%02d", hour, minute, second); } msleepTime 获取不到值。(应该获取到的是和GetSharedPreferences() 中mSleepTime的值相等才正确。)
- Java code
package wistron.mojo.boot; import java.util.Timer; import java.util.TimerTask; import wistron.mojo.R; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; /* * After the countdowntime has expired,start detectedd device,at the same time display the progress. * and update data to SETTING_Infos.xml file. */ public class UpdateProgress extends Activity implements OnClickListener{ public static final String SETTING_Infos = "SETTING_Infos"; public static final String BootNum = "BootNum"; public static final String SleepTime = "SleepTime"; public static final String mPowerType="coldboot"; public static final String Successful_time="0"; private final static int TRACK_TEXTVIEW = 0; private final static int TRACK_PROGRESSBAR = 1; public int mSleepTime,mBootNum,mSuccessful_time; public int mSuccessfulBoot,mPowerCycleType; public Button mPauseButton,mCancelButton; public EditText mSuccessfulBootEdit,mCountdownSleepEdit; public TextView SleepTimeTV,BootNumTV,CountDownTV; public TextView tv; public ProgressBar mProgressusb,mProgressssd,pb; public Timer timer; Thread mThread; public boolean isRunning = false; boolean pauseText=true; private final int[][] mTrackSlotResource = { {R.id.txviewssd, R.id.progressssd}, {R.id.txviewusb, R.id.progressusb}, }; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.progress); InitialUI(); } /* * Initial UI: * Read from sharedPreference -- @GetSharedPreferences(); * Sleep -- @CountDownTime(); * @InitialProgress(); */ private void InitialUI() { findView(); SetListener(); GetSharedPreferences(); CountDownTime(); } private void findView() { mPauseButton = (Button) findViewById(R.id.Pause); mCancelButton = (Button) findViewById(R.id.Cancel); BootNumTV=(TextView)findViewById(R.id.BoottimesTV); CountDownTV = (TextView) findViewById(R.id.CountdownTimeTV); } private void SetListener() { mPauseButton.setOnClickListener(this); mCancelButton.setOnClickListener(this); } private void GetSharedPreferences() { SharedPreferences settings = getSharedPreferences(SETTING_Infos, 0); mBootNum=settings.getInt(BootNum,1); mSleepTime=settings.getInt(SleepTime,1); mSuccessful_time=settings.getInt(Successful_time, 0); BootNumTV.setText("BootNum is"+String.valueOf(mBootNum)); } /* * Button listener action:@hRunBootTestListener(View button) * suspend and resume detected device action by button text value. */ private void hRunBootTestListener(View button) { switch(button.getId()){ case R.id.Pause:{ if(pauseText){ mPauseButton.setText("Start"); pauseText=false; }else if(!pauseText){ mPauseButton.setText("Pause"); pauseText=true; } }break; case R.id.Cancel:{ Log.i("Cancel button is clicked","cancle button"); }break; } } /* * @handler:Receives a different message value through handling of incidents * @CountDownTime() message.what=1 * @InitialProgress() message.what=2 */ final Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: CountDownTV.setText(getCountdownString()); break; case 2: for(int m=0;m<2;m++){ tv = (TextView) findViewById(mTrackSlotResource[m][TRACK_TEXTVIEW]); pb = (ProgressBar) findViewById(mTrackSlotResource[m][TRACK_PROGRESSBAR]); int progress=pb.getProgress(); if(progress==pb.getMax()){ tv.setText("Checking is completed "); }else if(progress>(3*(pb.getMax())/4)){ tv.setText("Checking completed 3/4..... "); }else if(progress>(2*(pb.getMax())/4)){ tv.setText("Checking usb completed 2/4..... "); }else if(progress>((pb.getMax())/4)){ tv.setText("Checking completed 1/4..... "); }else{ tv.setText("Checking is going..... "); } pb.incrementProgressBy(5); } break; } super.handleMessage(msg); } }; /* * @InitialProgress() * Initial Progressbar and sleep 1s every time */ private void InitialProgress() { mThread = new Thread(new Runnable() { @Override public void run() { Message message = new Message(); message.what = 2; for(int i=0;i<30&& isRunning;i++){ try { Thread.sleep(1000); } catch (InterruptedException e) { } } } }); } /* * @CountDownTime() : CountDownTime and display on UpdateProgress UI * This class includ Three method: * @CountDownTime() * TimerTask task * @getCountdownString():format string */ private void CountDownTime() { Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/digital-7 (mono).ttf"); CountDownTV.setTypeface(font); timer = new Timer("minutes-to-midnight"); timer.schedule(task,1000, 1000); //delay 1000ms excute,anmout of 1000ms in between subsequent executions /* * Display message "Start detected device" of toast */ Context context = getApplicationContext(); CharSequence text = "After"+mSleepTime+"s"+"Start detected device"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } TimerTask task = new TimerTask(){ public void run() { Message message = new Message(); message.what = 1; for(;mSleepTime>0;mSleepTime--){ handler.sendMessage(handler.obtainMessage(1)); } if(mSleepTime==0){ handler.sendMessage(handler.obtainMessage(2)); isRunning = true; mThread.start(); } } }; private String getCountdownString(){ int hour=00; int minute=00; int second = mSleepTime; return String.format("%02d:%02d:%02d", hour, minute, second); } @Override public void onClick(View button) { hRunBootTestListener(button); } }