1. CountDownTimer 是android 自己封装的定时,倒计时的类
主要是重写onTick和onFinsh这两个方法,onFinish()中的代码是计时器结束的时候要做的事情;onTick(Long m)中的代码是你倒计时开始时要做的事情,参数m是直到完成的时间,构造方法MyCount()中的两个参数中,前者是倒计的时间数,后者是倒计每秒中间 的间隔时间,都是以毫秒为单位。例如要倒计时30秒,每秒中间间隔时间是1秒,两个参数可以这样写MyCount(30000,1000)。 将后台线程的创建和Handler队列封装成为了一个方便的类调用。
当你想取消的时候使用mc.cancel()方法就行了
package cn.demo;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.TextView;
import android.widget.Toast;
public class NewActivity extends Activity {
private MyCount mc;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.show);
mc = new MyCount(30000, 1000);
mc.start();
}//end func
/*定义一个倒计时的内部类*/
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("finish");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("请等待30秒(" + millisUntilFinished / 1000 + ")...");
Toast.makeText(NewActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有显示时间延迟
}
}
}
2.
Handler与Runnable
package com.android.settings;
import java.util.ArrayList;
import com.android.settings.location.RadioButtonPreference;
import com.android.settings.location.RadioButtonPreference.OnClickListener;
import android.content.ContentResolver;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
public class BeidouDetailSettingsActivity extends PreferenceActivity implements
OnPreferenceClickListener {
private RadioButtonPreference mSelectGpsMode;
private RadioButtonPreference mSelectBeidouMode;
private RadioButtonPreference mSelectMixMode;
private CheckBoxPreference mPowerSaveMode;
private static final String GPS_MODE = "gps_mode";
private static final String BEIDOU_MODE = "beidou_mode";
private static final String MIX_MODE = "mix_mode";
private static final String TAG = "BeidouDetailSettingsActivity";
private static String KEY1_WORK_SAVE_MODE = "gps_power_save_mode";
private int recLen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.postDelayed(runnable, 1000);
}
@Override
protected void onResume() {
super.onResume();
createPreferenceHierarchy();
updateLocationToggles();
}
private void updateLocationToggles() {
// TODO Auto-generated method stub
int mode = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.BEIDOU_NAVIGATION_MODE, -1);
int saveMode = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.GPS_POWER_SAVING_MODE, -1);
if (saveMode == 1) {
mPowerSaveMode.setChecked(true);
} else {
mPowerSaveMode.setChecked(false);
}
if (mode == 0) {
mSelectGpsMode.setChecked(false);
mSelectBeidouMode.setChecked(true);
mSelectMixMode.setChecked(false);
} else if (mode == 1) {
mSelectGpsMode.setChecked(true);
mSelectBeidouMode.setChecked(false);
mSelectMixMode.setChecked(false);
} else {
mSelectGpsMode.setChecked(false);
mSelectBeidouMode.setChecked(false);
mSelectMixMode.setChecked(true);
}
}
private PreferenceScreen createPreferenceHierarchy() {
PreferenceScreen root = getPreferenceScreen();
if (root != null) {
root.removeAll();
}
addPreferencesFromResource(R.xml.beidou_detail_preference_settings);
root = getPreferenceScreen();
/**
* add by xuhong.tian
*/
mPowerSaveMode = (CheckBoxPreference) root
.findPreference(KEY1_WORK_SAVE_MODE);
mSelectGpsMode = (RadioButtonPreference) root.findPreference(GPS_MODE);
mSelectBeidouMode = (RadioButtonPreference) root
.findPreference(BEIDOU_MODE);
mSelectMixMode = (RadioButtonPreference) root.findPreference(MIX_MODE);
mPowerSaveMode.setOnPreferenceClickListener(this);
mSelectMixMode.setOnPreferenceClickListener(this);
mSelectBeidouMode.setOnPreferenceClickListener(this);
mSelectGpsMode.setOnPreferenceClickListener(this);
return root;
}
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
Log.d(TAG, "preference==" + preference);
if (preference == null) {
mSelectGpsMode.setChecked(false);
mSelectBeidouMode.setChecked(false);
mSelectMixMode.setChecked(false);
return true;
} else if (preference == mSelectGpsMode) {
mSelectGpsMode.setChecked(true);
mSelectBeidouMode.setChecked(false);
mSelectMixMode.setChecked(false);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.BEIDOU_NAVIGATION_MODE, 1);
return true;
} else if (preference == mSelectBeidouMode) {
mSelectGpsMode.setChecked(false);
mSelectBeidouMode.setChecked(true);
mSelectMixMode.setChecked(false);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.BEIDOU_NAVIGATION_MODE, 0);
return true;
} else if (preference == mSelectMixMode) {
mSelectGpsMode.setChecked(false);
mSelectBeidouMode.setChecked(false);
mSelectMixMode.setChecked(true);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.BEIDOU_NAVIGATION_MODE, 2);
return true;
} else if (preference == mPowerSaveMode) {
if (mPowerSaveMode.isChecked()) {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.GPS_POWER_SAVING_MODE, 1);
return true;
} else {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.GPS_POWER_SAVING_MODE, 0);
return true;
}
}
return false;
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
mSelectMixMode.setSummaryOn(String.valueOf(recLen));
// handler.postDelayed(this, 1000);
}
};
}Handler与Thread(不占用UI线程)