有没有办法在只执行一次方法后停止运行?
我创建了这段代码来延迟方法“getPreferences”的启动,但是当我运行应用程序时,“getPreferences”发送给我的下一个活动给出了屏幕刷新的问题?
这是否归结为可运行对象仍在继续循环,以及如何在一次执行后将其杀死?
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
public class StartScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getPreferences();
}}, 10000);
}
private void getPreferences() {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String name = sharedPreferences.getString("NAME", "");
if (name != null) {
// the key does not exist
Intent intent=new Intent(StartScreen.this,InitialPreferences.class);
startActivity(intent);
}
if (name == "NAME"){
// handle the value
Intent intent=new Intent(StartScreen.this,MainActivity.class);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start_screen, menu);
return true;
}
}
InitialPreferences 活动...
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class InitialPreferences extends StartScreen implements OnClickListener {
EditText editText1;
Button button1;
TextView textView1;
RadioGroup radioGroup;
RadioButton radioPosistionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_preferences);
// waitTimer.cancel();
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editText1);
button1 = (Button)findViewById(R.id.button1);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
}
@Override
protected void onStart(){
//waitTimer.cancel();
LoadPreferences();
super.onStart();
}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String name = sharedPreferences.getString("NAME", "");
textView1.setText(name);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SavePreferences("NAME", editText1.getText().toString());
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radio button by returned id
radioPosistionButton = (RadioButton) findViewById(selectedId);
}
}