0

我计划在我的 android 应用程序中实施奖励系统,该应用程序的用户每天可以通过打开应用程序获得 100 积分。用户在某一天只能获得 100 积分,无论他们打开应用程序后多少倍。我不知道我该怎么做,例如,一个名为 WhatsChat 的应用程序,给用户每天打开应用程序的积分:

在此处输入图像描述

我正在尝试实现一个类似的系统,但每天打开应用程序只给用户 100 分。我知道我需要跟踪这一天,并可能使用SharedPreferences并记录每天的日期将其存储在本地存储中,然后将记录点的变量增加 100。

在伪代码中,它看起来像这样:

Set CreditsGained to 0

IF app launched for the first time on Current date THEN
CreditsGained = CreditsGained + 100
Else 
Do nothing

如何在我的应用程序中实现这样的系统?

4

4 回答 4

1

我可能会迟到回答您的问题..但是对于寻求奖励系统并且不希望服务器处理这些东西的其他人->

您可以检查用户设备中是否启用了自动日期和时间选项

**。然后根据响应,可以给用户奖励。

例如。

calendar= Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);



  todaystring= year+ "" + month + "" + day + "";
   timepref=context.getSharedPreferences("REWARD",0);
   currentday=timepref.getBoolean(todaystring,false);


 //Daily reward
        if (!currentday && isZoneAutomatic(context) && isTimeAutomatic(context)) { //currentday =false
            btnrwrd.setEnabled(true);
            btnrwrd.setText("Collect your daily reward!");
            btnrwrd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Daily reward granted!", Toast.LENGTH_SHORT).show();
                   // Do your stuff here
                    // saving the date
                    SharedPreferences sharedPreferences = context.getSharedPreferences("SAVING", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edt = sharedPreferences.edit();
                    edt.putInt("mypoints", Integer.valueOf(points.getText().toString()));
                    edt.apply();
                    Toast.makeText(context, String.valueOf(daily), Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor timedaily = timepref.edit();
                    timedaily.putBoolean(todaystring, true);
                    timedaily.apply();
                    btnrwrd.setText("Wait for 24 hrs");
                    btnrwrd.setEnabled(false);
                }
            });
}
else {
            Toast.makeText(context, "Your daily reward is over!", Toast.LENGTH_SHORT).show();
        }


    public static boolean isTimeAutomatic(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME, 0) == 1;
        } else {
            return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }

    public static boolean isZoneAutomatic(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.Global.getInt(c.getContentResolver(), Settings.Global.AUTO_TIME_ZONE, 0) == 1;
        } else {
            return android.provider.Settings.System.getInt(c.getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }

我希望它对你有帮助!支持答案并祝你好运:)

于 2018-08-14T11:55:37.560 回答
1

你在服务器上做。他们每天第一次发出网络请求时,总分增加 100 分。没有办法在客户端安全地做到这一点。

于 2017-01-20T18:31:16.753 回答
0

我建议您不要使用它Shared Preferences来保存您想要的数据。因为他们可以修改它。

只有一种方法可以保护它,你必须拥有自己的服务器来保存它。本地存储也不安全。

但是如果你想知道如何检查,为了学习,你也可以使用Shared Preferences

Step1:获取您当前的时间string

SimpleDateFormat sdf = new SimpleDateFormat("d-m-Y");
Calendar cal = Calendar.getInstance();
String dateInStr = sdf.format(cal.getTime());

步骤 2:检查启动活动。后onCreate()

SharedPreferences pre=getSharedPreferences("my_sp", MODE_PRIVATE);
SharedPreferences.Editor edit=pre.edit();

if (pre.getBoolean(dateInStr, false)){
   //they checked today
}else{
  //not today
  // use check function here
  edit.putBoolean(dateInStr, true);
  edit.commit();
}

好的,把第1步的代码放在第2步的代码上面,我只是为了方便理解把它分开。

Step3:您可以检查他们是否有超过1次的检查点。如果这是他们一天中第一次退房,我们就给他们加 100 分。

把它放在上面的 else 语句中edit.putBoolean(dateInStr, true);

//get prev_score from SP
long previous_score = pre.getLong("score", 0);
//add 100
previous_score = previous_score + 100;
//save back to SP
edit.putLong("score", previous_score);
于 2017-01-20T18:32:22.600 回答
0

每当用户打开应用程序时,检查本地存储中是否有任何最后一个信用日期可用,如果有,请验证当前日期比存储日期多一,然后将第二天的信用日期记入用户。

如果日期在存储中不可用,那么您可以假设它是第一天。是的,对于这个用例,您可以在 MainActivity 中执行此操作,因为您不会进行任何耗时的 api 调用。

我只是分享了这个想法,找出代码。这很简单。

于 2020-05-18T07:00:19.377 回答