android - 停止运行

标签 android loops handler delay runnable

有没有办法在只执行一次方法后停止 runnable?

我已经创建了这段代码来延迟方法“getPreferences”的启动,但是当我运行该应用程序时,“getPreferences”发送给我的下一个 Activity 会出现屏幕刷新问题吗?
这是因为 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 Activity ....

 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);


 }


 }

最佳答案

run 方法只执行一次。如果你想要一个永无止境的循环,你需要包含类似 while(true) 的东西。
更好地描述您的问题,因为我不太了解。是下一个 Activity 加载两次的问题吗?在什么情况下?您可以发布下一个 Activity 的代码吗?哪个 Activity 正在加载? InitialPreferences 还是 MainActivity?
请注意,通过将代码包含在 Activity 的 onCreate 中,每次重新创建 Activity 时都会执行一次(例如,当您将手机从横向更改为纵向时,Activity重新创建,再次调用 onCreate

编辑 1: 您遇到的主要问题是您的 InitialPreference 类扩展了 StartScreen,并且在您的 onCreate 方法中,您调用 super 将触发另一个 postDelayed ,启动另一个 InitialPreference 等等。
你有什么理由扩展 StartScreen 而不是 Activity 吗?我认为通过此更改,您的问题将得到解决。当然,如果你正在做一个设置 Activity ,我会建议使用 PreferenceActivity。您可以查看 documentation http://developer.android.com/guide/topics/ui/settings.html

关于android - 停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17801808/

相关文章:

go - golang 中的 http HandleFunc 参数

android - 如何将数字放入多个 TextView 中?

android - 跟踪 Android 手机的电池高效方式

c# - 我用哪个循环询问用户是否要继续使用C#程序

javascript - 循环内循环产生错误结果

c - 输入变量直到输入负数

java - Android 服务完全破坏了设备的性能?

Android自定义对话框按钮XML onClick错误

android - 应用发布到市场后与所有设备不兼容

android - ItemizedOverlay 和 Overlay 类之间的区别