Android 如何在UI上保持更新的文本视图(通过SharedReferences)?
我有一个TextView(txtCounter),每次运行服务时它都会自动更新。上述过程运行平稳,但每当我关闭应用程序并再次打开它时,TextView不会显示实际变量,只有在我再次运行服务后才会显示 我知道我需要在onCreate(或onResume?)方法中做一些事情,类似于从更新它的方法接收更新的TextView(在我的例子中,它是“UpdateUI”),但我不知道是什么 主要活动:Android 如何在UI上保持更新的文本视图(通过SharedReferences)?,android,sharedpreferences,Android,Sharedpreferences,我有一个TextView(txtCounter),每次运行服务时它都会自动更新。上述过程运行平稳,但每当我关闭应用程序并再次打开它时,TextView不会显示实际变量,只有在我再次运行服务后才会显示 我知道我需要在onCreate(或onResume?)方法中做一些事情,类似于从更新它的方法接收更新的TextView(在我的例子中,它是“UpdateUI”),但我不知道是什么 主要活动: public class MainActivity extends AppCompatActivity {
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();
}
在
onResume()
中调用以下方法:
在
onResume()
中调用以下方法:
嘿,我刚刚注意到,您不再需要将
intent
传递给updateUI()
,因为我们没有从中读取任何内容。嘿,我刚刚注意到,您不再需要将intent
传递给updateUI()
,因为我们没有从中读取任何内容。此方法与OP的updateUI()有何不同
method?@code peedientupdateUI
还更新了计数器
,他想通过他的一个服务重复调用该计数器。两个建议:1。使用一个方法名,使其与原始的updateUI()
方法更具区别。2.重构原始的updateUI()
方法以调用此方法以减少代码重复。@code pedurent true,请给我一点时间,让editsWorked像个符咒一样。非常感谢。此方法与OP的updateUI()
方法有何不同?@code peedientupdateUI
还更新计数器
,他希望仅从其服务之一重复调用该计数器。两个建议:1。使用一个方法名,使其与原始的updateUI()
方法更具区别。2.重构原始的updateUI()
方法以调用此方法以减少代码重复。@code pedurent true,请给我一点时间,让editsWorked像个符咒一样。非常感谢。
// 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);
}