eclipse 在线程上启动 Activity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28089803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:20:59  来源:igfitidea点击:

Start Activity on a thread

androideclipse

提问by user3698267

I have a Splash Activity. and I want to start a new Activity when the progressStatus reach its max value. My problem is i dont know where to locate the start intent. I have an error on my IF statement.

我有一个Splash Activity. 我想在 progressStatus 达到最大值时启动一个新的 Activity。我的问题是我不知道在哪里找到开始意图。我的IF statement.

    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();

on my manifest:

在我的清单上

<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>

My Splash activity is MainActivity Classthen NewMainActivity Classis the second activity.

我飞溅活动MainActivity ClassNewMainActivity Class是第二次活动。

采纳答案by icecream

try to check the while loop as simply as this

尝试像这样简单地检查 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();

then under the Thread.sleep()put your condition.

然后根据Thread.sleep()你的条件。

回答by Alexios Karapetsas

You have to call the startActivity(intent) from the UI thread. You can just create a new method like the following:

您必须从 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);
      }
   });
}

The Looper.getMainLooper() verifies that this will be executed on the Main thread.

Looper.getMainLooper() 验证这将在主线程上执行。

Then your code will look like this:

然后您的代码将如下所示:

  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();

回答by user1140237

Just Send empty message to Handler when progress reached to max level.From run u cant directly start activity . you need to do it in ui thread

当进度达到最大级别时,只需向处理程序发送空消息。从运行开始,您无法直接启动活动。你需要在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();

回答by EpicPandaForce

Use Explicit Intent.

使用显式意图。

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

EDIT: Try like this

编辑:试试这样

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);
         }
     });
}