最近部门主管给了我一个小任务。将基于android2.3系统的手机上的收音机进行改装,其中有个设置睡眠时间的小功能。帖上来分享分享!
先贴图。

package com.huanghua;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SleepTime extends Activity implements OnClickListener{
public static final int GIALOG_SLEEP = 1;
public static final int COME_SLEEP = 2;
private Button btn = null;
private int mSleep = 0;
private Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == btn){
showDialog(GIALOG_SLEEP);
}
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dia = null;
switch(id){
case GIALOG_SLEEP:
Builder b = new AlertDialog.Builder(this);
b.setIcon(R.drawable.ic_menu_sleep);
b.setTitle("选择睡眠时间");
b.setSingleChoiceItems(
R.array.sleeptime,
mSleep,
new DialogInterface.OnClickListener() {
//在对话框中的按钮被点击时,这个方法将被调用
@Override
public void onClick(DialogInterface dialog, int which) {
mSleep = which;
}
}).setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setSleepTime(mSleep);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeDialog(GIALOG_SLEEP);
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
//对话框被取消时的回调方法
@Override
public void onCancel(DialogInterface dialog) {
removeDialog(GIALOG_SLEEP);
}
});
dia = b.create();
break;
case COME_SLEEP:
dia = new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("睡眠时间到!是否退出?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
removeDialog(COME_SLEEP);
}
}).create();
break;
default:break;
}
return dia;
}
/**
* 设置睡眠时间
* @param time
*/
private void setSleepTime(int time){
this.mSleep = time;
if(this.handler != null){
//将mRunnable从消息队列中移除
this.handler.removeCallbacks(mRunnable);
}
if(this.mSleep!=0){
handler = new Handler();
if(this.mSleep < 3){
//在指定的时间流逝之后,将mRunnable加入消息队列
this.handler.postDelayed(mRunnable, this.mSleep*1000 * 60 * 30);
}else{
this.handler.postDelayed(mRunnable, 4*1000 * 60 * 30);
}
}
Log.i("myDebug","show sleep dialog mSleep ="+this.mSleep);
}
/**
* 睡眠时间到了时调用该线程
*/
private Runnable mRunnable = new Runnable(){
@Override
public void run() {
//这里面的内容可以自己去实现,看需求了,可以是关机,也可以是关程序。
showDialog(COME_SLEEP);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SleepTime!</string>
<string name="app_name">SleepTime</string>
<string-array name="sleeptime">
<item><string name="off">"关闭"</string></item>
<item><string name="halfhourlater">"半小时后"</string></item>
<item><string name="anhourlater">"一小时后"</string></item>
<item><string name="twohourlater">"两小时后"</string></item>
</string-array>
</resources>
1101

被折叠的 条评论
为什么被折叠?



