关闭后保存激励广告价值

Save rewarded ad value after close

观看奖励广告视频后,文本视图显示奖励积分。每个视频后积分累积。但是,当应用程序关闭时,这些点将被删除。我希望这些点在用户返回应用程序时继续显示。我不知道这是 onDestroy 方法还是 savedinstance 的一部分。

    public class ProfileActivity extends AppCompatActivity implements RewardedVideoAdListener {

    TextView mText, userEmail;
    private int pointCount;

    private TextView dateTimeDisplay;
    private Calendar calendar;
    private SimpleDateFormat dateFormat;
    private String date;

    Button userLogout;
    Button goToHome;
    ImageView ivQR;
    private AdView mAdView;
    FirebaseAuth firebaseAuth;
    FirebaseUser firebaseUser;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    private RewardedVideoAd mRewardedVideoAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

     //rewards ads
        //ca-app-pub-9125010107042455/6647636731 actual
        //ca-app-pub-3940256099942544~3347511713 for testing
        MobileAds.initialize(getApplicationContext(), ("ca-app-pub-9125010107042455/6647636731"));
        // Use an activity context to get the rewarded video instance.
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
        mRewardedVideoAd.setRewardedVideoAdListener(this);

        //points count
        mText = findViewById(R.id.textView);
        pointCount = 0;
        mText.setText("Points: " + pointCount);

        loadRewardedVideoAd();

     private void loadRewardedVideoAd (){

        if (!mRewardedVideoAd.isLoaded()){
            //ca-app-pub-9125010107042455/6647636731 actual
            //ca-app-pub-3940256099942544/5224354917 for testing
            mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
        }
    }

    public void startVideoAd(View view){
        if(mRewardedVideoAd.isLoaded()){
            mRewardedVideoAd.show();
        }
    }

    @Override
    public void onRewardedVideoAdLoaded() {

    }

    @Override
    public void onRewardedVideoAdOpened() {

    }

    @Override
    public void onRewardedVideoStarted() {

    }

    @Override
    public void onRewardedVideoAdClosed() {

        loadRewardedVideoAd();

    }

    private void addPoints(int points) {
        pointCount +=  points;
        mText.setText("Points: " + pointCount);
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {

        Toast.makeText(ProfileActivity.this, " Points ", Toast.LENGTH_SHORT).show();
        addPoints(rewardItem.getAmount());

    }

    @Override
    public void onRewardedVideoAdLeftApplication() {

    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {

    }

    @Override
    protected void onPause() {
        mRewardedVideoAd.pause(this);
        super.onPause();
    }

    @Override
    protected void onResume() {
        mRewardedVideoAd.resume(this);
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        mRewardedVideoAd.destroy(this);
        super.onDestroy();
    }

    @Override
    public void onRewardedVideoCompleted() {

    }
}

您应该使用onSaveInstanceState(Bundle savedInstanceState)方法来保存点数。

您可以执行以下操作:

//This is what will be used to recognize your number of points in the saved bundle.
static final String POINTS = "pointCount";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save your number of points
    savedInstanceState.putInt(POINTS, pointCount);

    super.onSaveInstanceState(savedInstanceState);
}

然后调用onRestoreInstanceState(Bundle savedInstanceState)方法,这样就可以恢复点数了

public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // Restore your number of points and store them in your variable
    pointCount = savedInstanceState.getInt(POINTS);
}

如需进一步说明,请参考官方文档https://developer.android.com/guide/components/activities/activity-lifecycle#java

我建议为此使用 SharedPrefrence。

您可以使用此 class 以备将来使用。

create a class name: SharedPref & save this code in that class.

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPref
{
    private static SharedPreferences mSharedPref;
    public static final String POINTS = "POINTS";

    private SharedPref()
    {

    }

    public static void init(Context context)
    {
        if(mSharedPref == null)
            mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    }

    public static String read(String key, String defValue) {
        return mSharedPref.getString(key, defValue);
    }

    public static void write(String key, String value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putString(key, value);
        prefsEditor.commit();
    }

    public static boolean read(String key, boolean defValue) {
        return mSharedPref.getBoolean(key, defValue);
    }

    public static void write(String key, boolean value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putBoolean(key, value);
        prefsEditor.commit();
    }

    public static Integer read(String key, int defValue) {
        return mSharedPref.getInt(key, defValue);
    }

    public static void write(String key, Integer value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putInt(key, value).commit();
    }
}

只需在 MainActivity 上调用 SharedPref.init() 一次

SharedPref.init(getApplicationContext());

写入数据

SharedPref.write(SharedPref.POINTS, 25); //save 25 POINTS in shared preference.

读取数据

int POINTS = SharedPref.read(SharedPref.POINTS, 0); //read POINTS from shared preference, if no value found then it will return 0 as default POINTS.

感谢:How to use SharedPreferences in Android to store, fetch and edit values

根据您的代码,解决方案如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    SharedPref.init(getApplicationContext());
    // your other codes here.....

修改您的 addPoints 函数如下:

private void addPoints(int points) {
    pointCount +=  points;
    SharedPref.write(SharedPref.POINTS, pointCount);
    int POINTS = SharedPref.read(SharedPref.POINTS, 0); // you can make this globle once then use it anywhere in the class..
    mText.setText("Points: " + POINTS);
}

现在使用此代码显示总分:

int POINTS = SharedPref.read(SharedPref.POINTS, 0);
mText.setText("Points: " + POINTS);

您可以将累积的点数保存到数据库中,或者简单地使用 SharedPreferences 来保存和加载点数

节省积分

private void savePoints(int totalPoints){
    SharedPreferences prefs = mContext.getSharedPreferences(
            "user_points", Context.MODE_PRIVATE);
    // save points
    prefs.edit().putInt("points", totalPoints).apply();
}

加载保存的点

private int getPoints(){
    SharedPreferences prefs = mContext.getSharedPreferences(
            "user_points", Context.MODE_PRIVATE);
    // retrieve points
    // 0 is the default value if nothing was stored before
    return prefs.getInt("points", 0);
}

在您的 addPoints 方法中保存积分

private void addPoints(int points) {
pointCount +=  points;

// save points to SharedPrefs
savePoints(pointCount);

mText.setText("Points: " + pointCount);
}

最后在你的 onCreate() 改变这个

pointCount = 0;

至此

pointCount = getPoints();