【问题标题】:how to logout an user when date is changed更改日期时如何注销用户
【发布时间】:2017-10-16 10:17:05
【问题描述】:

我正在开发一个应用程序,当日期更改时用户需要注销。因为我正在使用 alarm manager 注销用户,但我无法做到这一点。他们是否有机会使用日期注销用户。

代码如下

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, Login.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar timeOff = Calendar.getInstance();

/* setting time */
timeOff.set(Calendar.HOUR_OF_DAY, 23);
timeOff.set(Calendar.MINUTE, 59);
timeOff.set(Calendar.SECOND, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);

【问题讨论】:

    标签: android date alarmmanager


    【解决方案1】:

    使用此操作

        final IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        /**eg
          this.sysForbidenReceiver.checkTime(this);
          this.registerReceiver(this.sysForbidenReceiver, filter);
        **/
    

    【讨论】:

    【解决方案2】:

    在您的第一个 Activity onCreateMethod 上使用此代码

    var currentDate = LocalDateEx.getNow().toString()
            Log.d("experiment",currentDate)
    
            var previousDate = Utility.getEDate(this)
            Log.d("dshf",previousDate)
            if (currentDate != previousDate){
    
                Utility.setEDate(this,currentDate)
                DBManager.deleteAllPlants(this)
                DBManager.deleteAllStates(this)
                DBManager.deleteAllBanks(this)
                DBManager.deleteAllDesignations(this)
    
                val intent = Intent(this, LoginActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            }
            else{
                Utility.setEDate(this,currentDate)
            }
    

    使用它来将您的日期存储在共享偏好中

    fun setEDate(context: Context, eDate: String) {
                context.getSharedPreferences("", 0).edit().putString(DATE, eDate).apply()
            }
    

    使用此代码从共享偏好中获取日期

    fun getEDate(context: Context): String? {
                return context.getSharedPreferences("", 0).getString(DATE, priviousDateDefault)
            }
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2021-01-28
      相关资源
      最近更新 更多