[英]How do I change image and after rerunning the main activity in the thread after 5 sec not go back to the basic layout in android
So I am creating an app where I get the string from my AsyncTask
which I have created as a subclass in my MainActivity
class in order to get the variables I receive from the internet. 因此,我正在创建一个应用程序,在其中我从
AsyncTask
获取字符串,该字符串是我在MainActivity
类中创建的子类,以便获取从Internet接收的变量。 And according to variables I get I change the images accordingly after every 5 seconds. 根据变量,我每5秒相应地更改一次图像。 Now the task is successful but on activity refresh I keep getting the default layout I created in
activity_main.xml
and again it changes to the one I want. 现在,任务已成功完成,但是在刷新活动时,我会继续获取在
activity_main.xml
创建的默认布局,然后再次更改为所需的布局。 Posting my code below. 在下面发布我的代码。
Thread thread = new Thread() { //thread I am running every 5 secs
@Override
public void run() {
try {
synchronized (this) {
wait(5000);
syncapp sa = new syncapp(); //AsyncTask to get String from Internet
sa.execute();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class); //Creating intent to restart the activity (Need a workaround if possible)
startActivity(mainActivity);
}
;
};
thread.start();
}
public void setImage(String str) //Function I will call to change Image
{
vacancy =0;
b = a.toCharArray();
for (int i = 0; i < 6; i++)
if (b[i] == '0'){
iv[i].setImageResource(R.drawable.img2);
vacancy++;}
else if (b[i] == '1') {
iv[i].setImageResource(R.drawable.img1);
}
Log.i("abc ", a);
tv.setText("InBackGround" + str);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/* for(int i =0;i<6;i++) {
outState.putChar("imv"+(i+1), b[i]);
}*/
outState.putString("a",a); //Getting a from the internet (php file)
Log.i("Saved State", "Activity A - Saving instance state");
}
setImageResource
to img2) permanently. setImageResource
到img2的代码中一样)。 I have already used onSaveInstanceState
But as I am taking values from the internet I don't know I am not able to use them. 我已经使用过
onSaveInstanceState
但是当我从互联网上获取值时,我不知道我无法使用它们。
So first of all.. when working with UI elements such as Views, Widgets, you would want to avoid spawning your own threads, as View can be touched only from the thread it was created from. 所以首先..当使用诸如Views,Widgets之类的UI元素时,您要避免产生自己的线程,因为只能从创建它的线程中触摸View。
Second.. you would also want to avoid sleeping inside your worker thread - basically just use the Handler class (from android.os) and post a delayed Runnable, like so: https://stackoverflow.com/a/20784353/2102748 . 其次..您还希望避免在您的工作线程中休眠-基本上只使用Handler类(来自android.os)并发布延迟的Runnable,例如: https : //stackoverflow.com/a/20784353/2102748 。 Just be sure to stop all work on that particular handler when your Activity stops, like so: https://stackoverflow.com/a/3627399/2102748
只要确保活动停止,就停止在该特定处理程序上的所有工作,例如: https : //stackoverflow.com/a/3627399/2102748
Third - you should perhaps load the photo immediately (on the same AsyncTask or thread) if that is the only thing you need, but be sure to post the "set bitmap to view" work to the handler you created. 第三-唯一需要的就是您可能应该立即将照片加载(在同一AsyncTask或线程上),但是请确保将“设置位图以查看”工作发布到您创建的处理程序中。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.