简体   繁体   English

正确地从非活动类开始一个意图

[英]Correctly starting an intent from non-activity class

My app is an OpenGL ES 2.0 app, and therefore, has the 2 required threads (UI Thread and the GL Thread) 我的应用程序是一个OpenGL ES 2.0应用程序,因此,有2个必需的线程(UI线程和GL线程)

I have a class that extends Activity and from there I can easily start intents like so: 我有一个扩展Activity的类,从那里我可以很容易地开始这样的意图:

Public class MainActivity extends Activity{

    public void goToSomeWebsite(){

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress)));

    }
}

Now, I have another class which is a basically a Scene and I have an onTouchEvent method in that class as well as a render method and some other methods. 现在,我有另一个类,它基本上是一个Scene,我在该类中有一个onTouchEvent方法,以及一个render方法和一些其他方法。 So, onTouchEvent runs on the UI thread and the render method (and others), run on the GL thread. 因此,onTouchEvent在UI线程和render方法(以及其他)上运行,在GL线程上运行。

So my class would be something like this: 所以我的班级会是这样的:

public class MyScene implements Scene(){

    @Override
    public void render(){
        //Render something here
    }

    @Override
    public void updateLogiC(){
        //Do some other work here
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        //Handle touch events
        return false;
    }
}

From the above class, what would be the correct (and safe) way to start this other intent? 从上面的课程中,开始这个其他意图的正确(和安全)方式是什么?

Does it matter whether I start it from the GL Thread (say from updateLogic above) or from the UI Thread (as in the onTouchEvent method above)? 我是从GL Thread(比如上面的updateLogic)还是从UI Thread(如上面的onTouchEvent方法)开始的,这有关系吗?

Currently, I have a handle to the Activity class and simply do something like this: 目前,我有一个Activity类的句柄,只需执行以下操作:

activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress)));

And it works fine (this is done from the GL thread), however, something tells me this isn't quite the correct way to accomplish this. 并且它工作正常(这是从GL线程完成的),然而,有些事情告诉我这不是完成此任务的正确方法。

If your class provide context , application etc you can easily do like this 如果您的班级提供上下文,应用程序等,您可以轻松地这样做

getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/")));

or 要么

getApplication().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com/")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM