这看起来像是一个隐藏在另一个问题中的问题,但是要解决第一级问题,您可以尝试使用CountDownLatch
而不是静态布尔值:
CountDownLatch latch = new CountDownLatch(1);
fetchNowMovie process = new fetchNowMovie(latch);
process.execute();
latch.await();
Intent i = new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(i);
您必须接受闩锁作为AsyncTask构造函数的一部分:
private final CountDownLatch latch;
public fetchNowMovie(CountDownLatch latch) {
this.latch = latch;
}
// ...
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
latch.countDown();
SplashScreenActivity.nowMovie.clear();
SplashScreenActivity.nowMovie.addAll(list);
}
,
尝试使用非常简单的库:
https://github.com/Arasthel/AsyncJobLibrary
只需启动您的Splash活动:
__getitem__()
在Splash活动的“ onCreate”方法中,在后台以及任务完成后需要在主线程上执行(更新列表):
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<scope>compile</scope>
<version>7.0</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.15</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.15</version>
<scope>compile</scope>
</dependency>
</dependencies>
连接库(app-project目录中的build.gradle):
int gcd(int p,int q)
{
if(p<q){gcd(q,p);}
if(q==0)
{
cout<<"p: "<<p<<endl; //This prints out 7
return p;
}
else gcd(q,p%q);
}
int main()
{
int n;
int count=0;
cin>>n;
while(n--)
{
count++;
cout<<"Pair #"<<count<<": ";
string input,input2;
cin>>input>>input2;
int sum1=0,sum2=0;
int g;
for(int i=0;i<input.size();i++)
{
if(input[i]-'0'==1)
sum1+=pow(2,input.size()-1-i);
}
for(int i=0;i<input2.size();i++)
{
if(input2[i]-'0'==1)
sum2+=pow(2,input2.size()-1-i);
}
cout<<sum1<<" "<<sum2<<endl;
g = gcd(sum1,sum2);
cout<<"g: "<<g<<endl; //but this print out 3
}
}
,
让我们使用简单的interface
逻辑以安全的方式完成您想做的事情:
因此,我们添加了简单的interface
,然后像这样重新定义了MyAsnycTask
类的constructor
:
public class MyAsnycTask extends AsyncTask
{
OnTaskFinished listener;
// Our simple interface
public interface OnTaskFinished {
void TimeToNextActivity();
}
// Your MyAsnycTask class constructor
public MyAsnycTask(OnTaskFinished l) {
listener = l;
}
. . .
作为onPostExecute()
中的最后一行代码,我们已经完成了所有工作。因此,请通过我们的listener
告诉我们:
listener.TimeToNextActivity();
要使用我们之前添加的interface
,您的Activity
必须implements
。因此我们实现了它。在实现的方法中,我们将Activity
移至下一个Intent
:
public class MyActivity extends Activity
implements MyAsnycTask.OnTaskFinished
{
@Override
public void TimeToNextActivity()
{
// Here go to next activity
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
}
在修改MyAsnycTask
类的constructor
时,我们必须像这样初始化它:
MyAsnycTask process = new MyAsnycTask(this);
process.execute();
本文链接:https://www.f2er.com/3164605.html