java - 显示 2 个 Activity 之间的进度条

标签 java android

我有 2 个 Activity ,一个叫 MainActivity,另一个叫 Circle。当我单击 MainActivity 上的按钮以启动第二个按钮时,我希望出现一个进度条加载屏幕。这是我目前拥有的代码,但它只会导致应用程序崩溃。

public class LoadingScreenActivity 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 the separate thread
    @Override
    protected void onPreExecute() 
    {
        //Create a new progress dialog
        progressDialog = new ProgressDialog(LoadingScreenActivity.this);
        //Set the progress dialog to display a horizontal progress bar 
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //Set the dialog title to 'Loading...'
        progressDialog.setTitle("Loading...");
        //Set the dialog message to 'Loading application View, please wait...'
        progressDialog.setMessage("Loading application View, please wait...");
        //This dialog can't be canceled by pressing the back key
        progressDialog.setCancelable(false);
        //This dialog isn't indeterminate
        progressDialog.setIndeterminate(false);
        //The maximum number of items is 100
        progressDialog.setMax(100);
        //Set the current progress to zero
        progressDialog.setProgress(0);
        //Display the progress dialog
        progressDialog.show();
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        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(850);
                    //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) 
    {
        //close the progress dialog
        progressDialog.dismiss();
        //initialize the View
        setContentView(R.layout.content_circle);
    }   
}

最佳答案

您可以在默认可见的第二个 Activity XML 中添加一些默认 View ,如进度条。当您加载数据或您所做的任何事情时,将其设置为 view.GONE。像这样的好图书馆:

https://github.com/81813780/AVLoadingIndicatorView

在您的 second_activity.xml 中使用:

 <com.wang.avi.AVLoadingIndicatorView
        android:layout_gravity="center"
        android:id="@+id/avloadingIndicatorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:indicatorName="BallPulse"
        app:indicatorColor="@color/myPrimaryColor"/>

然后在您的 Activity onCreate() 方法中:

  public void onCreate(Bundle savedInstanceState) {            
            super.onCreate(savedInstanceState);
loader= (AVLoadingIndicatorView) findViewById(R.id.avloadingIndicatorView);
    }

最后,当你完成加载时,只需使用:

 loader.setVisibility(View.GONE);

关于java - 显示 2 个 Activity 之间的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002159/

相关文章:

java - 为同一对象定义多个哈希函数

java - NoSuchMethodError 错误创建 session 工厂

Android后台服务崩溃而它滑出

android - 如何修复 Android 开发中的 Error Parsing XML?

java - 我可以为每个 servlet 使用不同的 SSL 证书吗?

java - 如何检索 IoT 中心数据的属性而不仅仅是实际负载

java - Cloud Foundry 应用程序是否可以下载 list 文件中的 Jar 文件?

java表达式查找多个单词

android - 如何使用 Stetho 查看 android 中的 Realm 数据库表?

android - 在 Android selinux 中转义冒号字符