我的活动正在使用以下onCreate()和带有while循环的Runnable线程。这里是代码:Android可运行线程卡住某处
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_user_registration);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sp.edit();
//Some Async task here
final File destFile = new File(filesDir, sp.getString(Constants.SharedPreferences.USER_ID,null)+ Constants.S3Bucket.IMAGE_EXTENSION);
final File downFile = new File(filesDir, Constants.S3Bucket.USER_DIRECTORY + sp.getString(Constants.SharedPreferences.USER_ID,null)+ Constants.S3Bucket.IMAGE_EXTENSION);
Runnable myRunnable = new Runnable() {
@Override
public void run() {
Log.v(LOG_TAG,"S3 file profile exists "+downFile.getAbsolutePath()+" "+downFile.exists());
while(!downFile.exists()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(LOG_TAG,"Error",e);
}
Log.v(LOG_TAG,"Inside while loop");
}
Log.v(LOG_TAG,"S3 file profile exists after while "+downFile.getAbsolutePath()+" "+downFile.exists());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
};
Thread myThread = new Thread(myRunnable);
myThread.start();
}
线程基本上检查正在由AWS服务创建的downFile。我的活动继续运行,并且控制不会在循环中输入。该条件第一次是真实的,之后控制器既不进入while循环也不退出。 while循环之前的语句正确记录。所以,基本上MainActivity并没有开始。
这是一个错误的线程执行?我不明白这里发生了什么。
如果我停止应用程序并再次运行它,则现在条件为false,因此控件不会在while循环中输入,但下一个Activity会正确加载。
谢谢!这也有效。 –
很高兴听到! :d –