android实现简单定时关机应用程序
2012-11-20 15:11:29 来源:WEB开发网核心提示:save button响应事件 class button_saveListener implements OnClickListener { @Override public void onClick(View v) { SharedPreferences sharedPreference
save button响应事件
class button_saveListener implements OnClickListener
{
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if(edit_hour.getText().toString().equals("")||edit_minute.getText().toString().equals(""))
Toast.makeText(ShutdownActivity.this, "have no shutdown time", Toast.LENGTH_SHORT).show();
else
{
editor.putInt("hour",Integer.parseInt(edit_hour.getText().toString()));
editor.putInt("minute",Integer.parseInt(edit_minute.getText().toString()));
editor.commit();
Toast.makeText(ShutdownActivity.this, "save success", Toast.LENGTH_SHORT).show();
}
text_time.setText("SHUTDOWN TIME :"+Integer.parseInt(edit_hour.getText().toString())+":"+Integer.parseInt(edit_minute.getText().toString()));
Intent intent =new Intent();
intent.setClass(ShutdownActivity.this, ServiceActivity.class);//要建议个Service
startService(intent);
}
}
{
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
if(edit_hour.getText().toString().equals("")||edit_minute.getText().toString().equals(""))
Toast.makeText(ShutdownActivity.this, "have no shutdown time", Toast.LENGTH_SHORT).show();
else
{
editor.putInt("hour",Integer.parseInt(edit_hour.getText().toString()));
editor.putInt("minute",Integer.parseInt(edit_minute.getText().toString()));
editor.commit();
Toast.makeText(ShutdownActivity.this, "save success", Toast.LENGTH_SHORT).show();
}
text_time.setText("SHUTDOWN TIME :"+Integer.parseInt(edit_hour.getText().toString())+":"+Integer.parseInt(edit_minute.getText().toString()));
Intent intent =new Intent();
intent.setClass(ShutdownActivity.this, ServiceActivity.class);//要建议个Service
startService(intent);
}
}
我们再来看看Service类:
public void onCreate() {
SharedPreferences myshaPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
hour=myshaPreferences.getInt("hour", -1);
minute=myshaPreferences.getInt("minute", -1);
Thread thread=new Thread(){
public void run()
{
while(true)
{
Calendar calendar=Calendar.getInstance();
h=calendar.getTime().getHours();
m=calendar.getTime().getMinutes();
if(h==hour&&m==minute)
{
Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);//这里是会报错的,所以不能在eclipse下编译,要在源码下编译(下面会介绍)
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
break;
}
try {
Thread.sleep(1000);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
};
thread.start();
super.onCreate();
}
SharedPreferences myshaPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
hour=myshaPreferences.getInt("hour", -1);
minute=myshaPreferences.getInt("minute", -1);
Thread thread=new Thread(){
public void run()
{
while(true)
{
Calendar calendar=Calendar.getInstance();
h=calendar.getTime().getHours();
m=calendar.getTime().getMinutes();
if(h==hour&&m==minute)
{
Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);//这里是会报错的,所以不能在eclipse下编译,要在源码下编译(下面会介绍)
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
break;
}
try {
Thread.sleep(1000);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
};
thread.start();
super.onCreate();
}
更多精彩
赞助商链接