在线程上启动Activity

5

我有一个“启动画面活动(Splash Activity)”,当进度状态达到最大值时,我想要启动一个新的活动(Activity)。我的问题是我不知道在哪里放置启动意图。我的 IF 语句 出现了错误。

    new Thread(new Runnable() {
        public void run() {
                while (progressStatus < 100) {
                progressStatus += 5;

                }   
                if (progressStatus == progressBar.getMax()) {
                        Intent intent = new Intent(".MENU");
                        startActivity(intent);
                    }
                // Update the progress bar and display the
                // current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/"
                                + progressBar.getMax());
                    }
                });

                try {
                    // Sleep for 200 milliseconds.
                    // Just to display the progress slowly

                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }).start();

在我的清单上:

<activity
android:name="NewMainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
       <category android:name="android.intent.category.DEFAULT" />
       <action android:name=".MENU" />
</intent-filter>
</activity>

我的Splash活动是MainActivity类,然后NewMainActivity类是第二个活动。

4个回答

10

您必须从UI线程中调用startActivity(intent)。 您可以创建一个新方法,如下所示:

public void startActivityFromMainThread(){

   Handler handler = new Handler(Looper.getMainLooper());
   handler.post(new Runnable() {
   @Override
      public void run() {
          Intent intent = new Intent (MainActivity.this, NewMainActivity.class);
          startActivity(intent);
      }
   });
}
Looper.getMainLooper()验证这将在主线程上执行。然后,您的代码将如下所示:
  new Thread(new Runnable() {
    public void run() {
            progressBar.setMax(100);
            progressStatus = 0;

            while (progressStatus < 100) {
               progressStatus += 5;            

               // Update the progress bar and display the
               // current value in the text view
               handler.post(new Runnable() {
                  @Override
                  public void run() {
                      progressBar.setProgress(progressStatus);
                      textView.setText(progressStatus + "/"
                              + progressBar.getMax());
                  }
               });


             try {
                 // Sleep for 200 milliseconds.
                 // Just to display the progress slowly

                 Thread.sleep(200);

              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
           startActivityFromMainThread();
        }

}).start();

先生,Logcat 显示“无法找到显式活动类”。 - user3698267
@user3698267,您想要启动的Activity类的名称是什么?它在清单文件中有指定吗? - user1140237
在上面的代码中,您必须使用要启动的活动的名称替换MenuActivity。例如,如果是“菜单”,则必须将其命名为:Menu.class。 - Alexios Karapetsas
先生,启动画面活动没有显示出来。当我启动应用程序时,它会跳到第二个活动。 - user3698267
那么似乎 ProgressBar 存在问题。尝试进行调试,查看更新情况以及何时 progressStatus >= 100。 - Alexios Karapetsas
显示剩余4条评论

2
尝试将while循环简化为以下内容。
new Thread(new Runnable() {
    @Override
    public void run() {
        while (progressStatus < 100) {
           progressStatus += 5;
           // Update the progress bar and display the 
           //current value in the text view
           handler.post(new Runnable() {
                @Override
                public void run() {
                   progressBar.setProgress(progressStatus);
                   textView.setText(progressStatus+"/"+progressBar.getMax());
                }
            });

            try {
               // Sleep for 200 milliseconds. 
               //Just to display the progress slowly
               Thread.sleep(200); 
               if (progressStatus == 100){
                   Intent intent = new Intent(".MENU");
                   startActivity(intent);
               }

            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         } // while loop
      } // run()
  }).start();

Thread.sleep()下面放置您的条件。


1

当进度达到最大级别时,只需向处理程序发送空消息。从运行中无法直接启动活动,您需要在UI线程中执行此操作。

private Handler handlerIntentStart = new Handler() {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.Handler#handleMessage(android.os.Message)
         */
        @Override
        public void handleMessage(Message msg) {

            // ****** Acitity class must be added in manifest
            startActivity(new Intent(MainActivity.this,
                    NewMainActivity.class));
        }

    };


new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 5;

                if (progressStatus == progressBar.getMax()) {
                    handlerIntentStart.sendEmptyMessage(0);
                }
                // Update the progress bar and display the
                // current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/"
                                + progressBar.getMax());
                    }
                });

                try {
                    // Sleep for 200 milliseconds.
                    // Just to display the progress slowly

                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();

启动下一个活动似乎可以工作,但问题是闪屏活动没有显示。 - user3698267
@user3698267,它显示了问题,可能是您给出的休眠时间太短了...而且现在它正在while循环内部。 - user1140237

0

使用显式意图。

if (progressStatus == progressBar.getMax()) {
        Intent intent = new Intent(CurrentActivity.this, MenuActivity.class);
        startActivity(intent);
}

编辑:尝试这样

if (progressStatus == progressBar.getMax()) {
     Handler handler = new Handler(Looper.getMainLooper());
     handler.post(new Runnable() {
         public void run() {
                Intent intent = new Intent(CurrentActivity.this, MenuActivity.class);
                CurrentActivity.this.startActivity(intent);
         }
     });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接