如何在UI上保留更新的TextView(通过SharedPreferences)?

时间:2016-08-16 17:28:19

标签: android sharedpreferences

我有这个TextView(txtCounter),它每次运行服务时都会自动更新。所述过程运行顺利,但每当我关闭应用程序并再次打开它时,TextView都不会显示实际变量,只有在我再次运行该服务之后。

我知道我需要在onCreate(或onResume?)方法中做一些事情,就像从更新它的方法接收更新的TextView一样(在我的例子中,它是"" UpdateUI& #34;)但我不知道是什么。

MainActivity:

public class MainActivity extends AppCompatActivity {

public TextView txtCounter;

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

    final Button startApp = (Button) findViewById(R.id.startApp);
    final EditText timer = (EditText) findViewById(R.id.insertTimer);

    assert startApp != null;
    startApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Countdown, Started", Toast.LENGTH_SHORT).show();

            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);

            Intent intent = new Intent(MainActivity.this, MainService.class);
            assert timer != null;
            intent.putExtra("timer", timer.getText().toString());
            startService(intent);

            Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);

            registerReceiver(broadcastReceiver, new IntentFilter(MainService.BROADCAST_ACTION));
        }
    });
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) { updateUI(intent); }
};

private void updateUI(Intent intent) {
    txtCounter = (TextView) findViewById(R.id.txtCounter);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
    int counter = pref.getInt("counter", 0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putInt("counter", ++counter);
    editor.apply();

    assert txtCounter != null;
    txtCounter.setText(String.valueOf(counter));
}

@Override
public void onDestroy() {
    try { unregisterReceiver(broadcastReceiver); } catch (IllegalArgumentException ignored) {}

    super.onDestroy();
}

1 个答案:

答案 0 :(得分:3)

onResume()中调用以下方法:

// read counter variable and update the textview
private void updateTextView() {
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
    int counter = pref.getInt("counter", 0);
    setCounterTV(counter);
}


// Update the textview with a counter value
private void setCounterTV(int counter) {
    txtCounter = (TextView) findViewById(R.id.txtCounter);
    assert txtCounter != null;
    txtCounter.setText(String.valueOf(counter));
}


// Method to be called by your service
private void updateUI() {
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyFile", MODE_PRIVATE);
    int counter = pref.getInt("counter", 0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putInt("counter", ++counter);
    editor.apply();

    setCounterTV(counter);
}