当android APP INACTIVE或按下主页按钮后,如何在(注销)5分钟后编码结束会话?我使用下面的代码......用于保存数据保存................................. ....................................
public class LogoutService extends Service {
public static CountDownTimer timer;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1, 1000) {//1 * 60 * 1000, 1000
public void onTick(long millisUntilFinished) {
//Some code
// Log.v(Constants.TAG, "Service Started");
}
public void onFinish() {
// Log.v(SyncStateContract.Constants.TAG, "Call Logout by Service");
// Code for Logout
dialogBox();
Log.i("fr","fr");
stopSelf();
}
};
}
public void dialogBox() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Your session has expired.");
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
// alertDialogBuilder.setNegativeButton("cancel",
// new DialogInterface.OnClickListener() {
//
// @Override
// public void onClick(DialogInterface arg0, int arg1) {
//
// }
// });
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
on MainActivity:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}
错误:
3-28 01:52:12.381 1250-1250/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com. . ., PID: 1250
java.lang.NullPointerException