当应用程序处于非活动状态或按下home按钮时5分钟后结束会话-android

iaqfqrcu  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(236)

当android应用程序处于非活动状态或按下home按钮时,如何在(注销)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;

 }
}

主要活动:

@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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题