Android实现每天8点页面跳转
public class MainActivity extends Activity {
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
Log.i("====","执行了");
//需要执行的代码放这里
Intent intent=new Intent(MainActivity.this,DemoActivity.class);
startActivity(intent);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
};
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");// 可以方便地修改日期格式
String hehe = dateFormat.format(now);
Timer timer = new Timer(true);
timer.schedule(task,strToDateLong(hehe+" 08:00:00"));
}
/**
* string类型时间转换为date
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
}