有没有办法在一个方法只执行一次之后就停止runnable?
我创建此代码是为了延迟"getPreferences“方法的启动,但是当我运行应用程序时,"getPreferences”发送给我的下一个活动会导致屏幕刷新吗?
这是不是到了runnable还在继续循环,我如何在一次执行后杀死它?
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);
}
}发布于 2013-07-23 13:06:43
如果你希望有一个永无止境的循环,你需要包括像while(true)这样的东西。
更好地描述你的问题,因为我不太理解它。下一个活动加载两次的问题是什么?在什么情况下?你能把你下一次活动的代码贴出来吗?正在加载的是哪项活动?InitialPreferences还是MainActivity?
请注意,通过将代码包含在活动的onCreate中,它将在每次重新创建活动时执行一次(例如,当您将手机从横向更改为纵向时,将重新创建活动,再次调用onCreate )。
编辑1:您遇到的主要问题是您的InitialPreference类扩展了StartScreen,并且在您的onCreate方法中,对super的调用将触发另一个postDelayed,启动另一个InitialPreference,依此类推。
你为什么要扩展StartScreen而不是Activity呢?我认为有了这个改变,你的问题就会得到解决。当然,如果您正在进行设置活动,我建议您使用PreferenceActivity。您可以查看documentation http://developer.android.com/guide/topics/ui/settings.html
发布于 2013-07-23 13:04:34
停止runnable的正确方法是终止其中的循环。在您的例子中,没有循环,所以runnable应该在getPreferences()结束运行后停止。
由于您已将其放置在onCreate中,因此在创建或重新创建活动后,它将运行10000毫秒
发布于 2013-07-23 14:14:38
对重复的任务使用Timer和TimerTask。
请参见下面的代码。这对我很管用。
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// use handler here.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getPreferences();
}
});
}
},100000) // second parameter of scheduleAtFixedRate() method is Time in MilliSecond
Specify the time interval here for repepititive execution.在活动的onDestroy或onStop方法中,使用以下代码取消计时器。
timer.cancel();https://stackoverflow.com/questions/17801808
复制相似问题