Android Studio-为不同用户保存TextView值



我的应用程序有一个在.net和SSMS(SQL Server(中制作的身份验证服务

在我的一项活动中,我想存储一个TextView值(由于用户使用应用程序,该值正在增加(,以便每个用户都有自己的值。例如,用户1使用应用程序1h并关闭该应用程序。如果用户2登录应用程序,则该值应不同。当用户1再次登录时,该值应该是他关闭应用程序之前的最后一个值。

在这种情况下,我应该使用哪种方法?我应该使用SharedPreferences吗?

编辑于2020年9月8日

UserPoints.java

public class UserPoints extends AppCompatActivity{
private TextView userMoneyTV;
private BroadcastReceiver minuteUpdateReceiver;
private int userMoney = 0;
private Button saveMoney, loadMoney;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_points);
userMoneyTV = (TextView) findViewById(R.id.userMoney);
final String UserMoney = userMoneyTV.getText().toString();

saveMoney = (Button) findViewById(R.id.saveMoney);
saveMoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("MYPREFS",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("money", UserMoney);
editor.putString("email", String.valueOf(R.id.email));
editor.commit();
}
});
loadMoney = (Button) findViewById(R.id.loadMoney);
loadMoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("MYPREFS",MODE_PRIVATE);
String money = preferences.getString("money",UserMoney);
String email = preferences.getString("email",String.valueOf(R.id.email));
userMoneyTV.setText(money);
}
});
}
public void startMinuteUpdateReceiver(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
minuteUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
userMoney++;
userMoneyTV.setText(""+ userMoney);
}
};
registerReceiver(minuteUpdateReceiver,intentFilter);
}
@Override
protected void onResume() {
super.onResume();
startMinuteUpdateReceiver();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(minuteUpdateReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}

activity_user_points.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rewardLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rewardInfoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<ImageView
android:id="@+id/chestReward"
android:layout_width="200dp"
android:layout_height="150dp"
android:layout_marginTop="120dp"
android:src="@drawable/chestreward"
android:layout_alignParentLeft="true">
</ImageView>
<TextView
android:id="@+id/rewardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginBottom="68dp"
android:layout_toRightOf="@+id/chestReward"
android:text="Earn money every minute you use the app"
android:layout_marginTop="175dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/moneyInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/rewardInfoLayout">
<TextView
android:id="@+id/userMoneyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="casual"
android:text="Your Money: "></TextView>
<TextView
android:id="@+id/userMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_toRightOf="@+id/userMoneyText"
android:layout_marginTop="20dp">
</TextView>
<TextView
android:id="@+id/euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="€"
android:layout_toRightOf="@+id/userMoney"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/moneyInfo">
<Button
android:id="@+id/saveMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Money"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:onClick="startVideoAd">
</Button>
<Button
android:id="@+id/loadMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Money"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/saveMoney">
</Button>
</RelativeLayout>
</RelativeLayout>

编辑9/2020

我的问题已通过EDIT 2020年8月9日解决

现在我的问题是,当我在另一项活动中时,如何使价值增加?异步任务?

是的,您可以使用SharedPreferences,但如果用户卸载应用程序,用户将丢失其数据和您存储在SharedPreferences中的值。如果你想在卸载应用程序后保存数据,你可以使用任何轻量级数据库,即RealmRoomSQLiteFirebase,并将本地数据库存储在用户sd卡中。否则,请使用谷歌备份服务或将其存储在其他地方,如服务器上。