如何从线程调用另一个活动?

时间:2014-06-30 08:41:56

标签: android multithreading

我在stackoverflow中尝试了相同的问题,但没有一个正在工作。有人请帮助我。 这是我的代码

 new Thread(new Runnable() {
                        public void run() {

                            try{
                                CallSoap cs;
                                cs=new CallSoap();
                               // String resp=cs.Call();
                               String resp= cs.Call();
                                ActivateDecive.rslt=resp;
                            }catch(Exception ex)
                            {ActivateDecive.rslt=ex.toString();}  


                            if(ActivateDecive.rslt=="0"||ActivateDecive.rslt.equals("0"))
                             {
                                 //Toast.makeText(getBaseContext(), "Device is not in active state..Please contact admin",Toast.LENGTH_LONG).show();
                                ActivationText.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        ActivationText.setText("Device is not in active state..Please contact admin");
                                    }
                                });

                             }
                             else
                             {
                                 Handler handler = new Handler(Looper.getMainLooper());
                                 handler.postDelayed(new Runnable() {

                                        @Override
                                        public void run() {
                                            // TODO Auto-generated method stub
                                            ActivationText.setText("Device is Active");
                                            Intent i=new Intent(ActivateDecive.this,FormSelectionACT.class);

                                            startActivity(i);
                                        }
                                    }, 2000);

                             }

                        }
                    }).start();

活动不会在顶部,只有包含的布局显示...

2 个答案:

答案 0 :(得分:0)

您必须从主线程启动活动。这样做:

Handler handler = new Handler(Looper.getMainLooper());

handler.post(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent (MyActivity.this, NextActivity.class);
        startActivity(intent);
    }
});

取自此处:How to start an activity from a thread class in android?

编辑否则尝试将处理程序变量设置为全局,因此它应该如下所示。

 public class MyActivity extends Activity
{
    Handler hander = new Handler(){
        public void handleMessage(Message m){
            Intent intent = new Intent (MyActivity.this, Next.class);
            startActivity(intent);
        }
    };
    pubilc void onCreate(Bundle ic)
    {
       //your code setContentView() etc....
       Thread toRun = new Thread()
       {
              public void run()
              {
                    hander.sendMessage(1); 
              }
       }
       toRun.start();
    }
}

答案 1 :(得分:0)

通过添加:

完成您的Handler方法
   ActivateDecive.this.finish();

以下

 startActivity(i);