如何开始从Android的线程类的活动? [英] How to start an activity from a thread class in android?

查看:100
本文介绍了如何开始从Android的线程类的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我伸出一个线程类,并从该类我要开始一个活动。如何做到这一点?

I am extending a thread class and from that class I want to start an activity. How to do this?

推荐答案

您需要调用 startActivity()应用程序的主线程上。做到这一点的方法之一是通过执行以下操作:

You need to call startActivity() on the application's main thread. One way to do that is by doing the following:

  1. 初始化处理程序,并将其与应用程序的主线程关联起来。

  1. Initialize a Handler and associate it with the application's main thread.

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

  • 裹在code将启动活动内部的匿名的Runnable 类,并通它的处理程序#后(可运行)方法。

  • Wrap the code that will start the Activity inside an anonymous Runnable class and pass it to the Handler#post(Runnable) method.

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

  • 这篇关于如何开始从Android的线程类的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆