Android 在线程上启动活动

Android 在线程上启动活动,android,eclipse,Android,Eclipse,我有一个Splash活动。我想在progressStatus达到其最大值时启动一个新活动。我的问题是我不知道从哪里开始。我的IF语句有一个错误 new Thread(new Runnable() { public void run() { while (progressStatus < 100) { progressStatus += 5; }

我有一个
Splash活动
。我想在progressStatus达到其最大值时启动一个新活动。我的问题是我不知道从哪里开始。我的
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();
new线程(new Runnable()){
公开募捐{
而(进度状态<100){
进展状态+=5;
}   
if(progressStatus==progressBar.getMax()){
意向意向=新意向(“菜单”);
星触觉(意向);
}
//更新进度条并显示
//文本视图中的当前值
handler.post(新的Runnable(){
公开募捐{
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+“/”
+progressBar.getMax());
}
});
试一试{
//睡眠200毫秒。
//只是为了慢慢地显示进度
睡眠(200);
}捕捉(中断异常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>


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

您必须从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);
      }
   });
}
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();
new线程(new Runnable()){
公开募捐{
设置最大进度条(100);
progressStatus=0;
而(进度状态<100){
进展状态+=5;
//更新进度条并显示
//文本视图中的当前值
handler.post(新的Runnable(){
@凌驾
公开募捐{
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+“/”
+progressBar.getMax());
}
});
试一试{
//睡眠200毫秒。
//只是为了慢慢地显示进度
睡眠(200);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
startActivityFromMainThread();
}
}).start();

当进度达到最大水平时,只需向处理程序发送空消息。从运行开始,您无法直接启动活动。您需要在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();
private handlerIntentStart=new Handler(){
/*
*(非Javadoc)
* 
*@see android.os.Handler#handleMessage(android.os.Message)
*/
@凌驾
公共无效handleMessage(消息消息消息){
//******必须在清单中添加权限类
startActivity(新意图)(MainActivity.this、,
NewMainActivity.class);
}
};
新线程(newrunnable()){
公开募捐{
而(进度状态<100){
进展状态+=5;
if(progressStatus==progressBar.getMax()){
handlerIntentStart.sendEmptyMessage(0);
}
//更新进度条并显示
//文本视图中的当前值
handler.post(新的Runnable(){
公开募捐{
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+“/”
+progressBar.getMax());
}
});
试一试{
//睡眠200毫秒。
//只是为了慢慢地显示进度
睡眠(200);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}).start();
使用明确的意图

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

试着像这样简单地检查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();
new线程(new Runnable()){
@凌驾
公开募捐{
而(进度状态<100){
进展状态+=5;
//更新进度条并显示
//文本视图中的当前值
handler.post(新的Runnable(){
@凌驾
公开募捐{
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+“/”+progressBar.getMax());
}
});
试一试{
//睡眠200毫秒。
//只是为了慢慢地显示进度
睡眠(200);
如果(progressStatus==100){
意向意向=新意向(“菜单”);
星触觉(意向);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}//while循环
}//运行()
}).start();

然后在
Thread.sleep()下放置您的条件

先生,logcat说,
无法找到显式活动类
@user3698267要启动的活动类的名称是什么?它是在清单中指定的吗?@user3698267在上面的代码中,您必须用要启动的活动的名称替换MenuActivity。例如,如果这是“Menu”,您必须将其命名为:Menu.classsir它可以工作。但是地点呢