Java Android启动屏幕异步任务
基本上,我有一个加载启动屏幕,单击按钮时将执行该屏幕:Java Android启动屏幕异步任务,java,android,splash-screen,Java,Android,Splash Screen,基本上,我有一个加载启动屏幕,单击按钮时将执行该屏幕: public void onClick(View v) { // Load the loading splash screen Intent loadingIntent = new Intent(context, LoadingScreen.class); context.startActivity(loadingIntent); } });
public void onClick(View v) {
// Load the loading splash screen
Intent loadingIntent = new Intent(context, LoadingScreen.class);
context.startActivity(loadingIntent);
}
});
在LoadingScreen类中:
public class LoadingScreen extends Activity{
//A ProgressDialog object
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Initialize a LoadViewTask object and call the execute() method
new LoadViewTask().execute();
}
//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
//Before running code in separate thread
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(LoadingScreen.this,"Getting routes...",
"Loading data, please wait...", false, false);
}
//The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params)
{
try
{
//Get the current thread's token
synchronized (this)
{
//Initialize an integer (that will act as a counter) to zero
int counter = 0;
//While the counter is smaller than four
while(counter <= 4)
{
//Wait 850 milliseconds
this.wait(750);
//Increment the counter
counter++;
//Set the current progress.
//This value is going to be passed to the onProgressUpdate() method.
publishProgress(counter*25);
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}
//Update the progress
@Override
protected void onProgressUpdate(Integer... values)
{
//set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}
//after executing the code in the thread
@Override
protected void onPostExecute(Void result)
{
finish();
//close the progress dialog
progressDialog.dismiss();
}
}
公共类加载屏幕扩展活动{
//ProgressDialog对象
私有进程对话;
/**首次创建活动时调用。*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//初始化LoadViewTask对象并调用execute()方法
新建LoadViewTask().execute();
}
//要使用AsyncTask,它必须是子类
私有类LoadViewTask扩展了AsyncTask
{
//在单独的线程中运行代码之前
@凌驾
受保护的void onPreExecute()
{
progressDialog=progressDialog.show(加载屏幕,这是“获取路线…”),
“正在加载数据,请稍候…”,false,false);
}
//要在后台线程中执行的代码。
@凌驾
受保护的Void doInBackground(Void…参数)
{
尝试
{
//获取当前线程的令牌
已同步(此)
{
//将整数(用作计数器)初始化为零
int计数器=0;
//而计数器小于4
而(counter在onClick()
方法中,您可以编写如下内容:
new LoadViewTask().execute();
进度对话框将显示在该页面中。你在做什么,伙计,只需调用你的异步任务,而不是意图
public void onClick(View v)
{
new LoadViewTask().execute();
}
});
你的意图在后期执行吗
@Override
protected void onPostExecute(Void result)
{
finish();
//close the progress dialog
progressDialog.dismiss();
//START YOUR ACTIVITY HERE
Intent loadingIntent = new Intent(context, LoadingScreen.class);
context.startActivity(loadingIntent);
}
必须阅读的文档才能解释?请检查此项,但这样做会首先显示启动屏幕,然后显示我的AsyncTask方法,加载数据花费了很长时间。是否有任何方法使两者同步?Intent loadingIntent=new Intent(上下文,LoadingScreen.class);context.startActivity(loadingcontent);从onClick()中删除这些行。只需在onClick()中调用新的LoadViewTask().execute()。希望这会有帮助。是的。你能帮我检查我编辑的部分吗?因为这样做,它将首先加载启动屏幕。加载完成后,它将在我的AsyncTask中执行该方法。是否有任何方法使它们都同步?你能帮我检查我编辑的部分吗?因为这样做,启动屏幕将显示为rst然后是我的AsyncTask方法。有没有办法在加载启动屏幕时,我也在AsyncTask中运行该方法?基本上,我在GetRegisteredEventAsyncTask中的方法加载数据需要很长时间,所以我使用的是启动加载屏幕。所以我尝试的是,当用户选择按钮时,它将加载启动启动屏幕,同时执行GetRegisteredEventAsyncTask中的方法。通过执行我上面提供的代码,它将首先加载启动屏幕。一旦完成加载,然后执行GetRegisteredEventAsyncTask中的方法。因此,启动加载屏幕似乎有点多余
@Override
protected void onPostExecute(Void result)
{
finish();
//close the progress dialog
progressDialog.dismiss();
//START YOUR ACTIVITY HERE
Intent loadingIntent = new Intent(context, LoadingScreen.class);
context.startActivity(loadingIntent);
}