简体   繁体   中英

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)

I have a class that extends Activity and from there I can easily start intents like so:

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. So, onTouchEvent runs on the UI thread and the render method (and others), run on the GL thread.

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

Currently, I have a handle to the Activity class and simply do something like this:

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.

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/")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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