【问题标题】:Correctly starting an intent from non-activity class从非活动类正确启动意图
【发布时间】:2015-09-06 18:43:19
【问题描述】:

我的应用是 OpenGL ES 2.0 应用,因此有 2 个必需的线程(UI 线程和 GL 线程)

我有一个扩展 Activity 的类,从那里我可以轻松地启动意图,如下所示:

Public class MainActivity extends Activity{

    public void goToSomeWebsite(){

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

    }
}

现在,我有另一个类,它基本上是一个场景,我在该类中有一个 onTouchEvent 方法以及一个渲染方法和一些其他方法。因此,onTouchEvent 运行在 UI 线程上,而 render 方法(和其他方法)运行在 GL 线程上。

所以我的班级应该是这样的:

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

从上面的课程中,启动这个其他意图的正确(和安全)方式是什么?

我是从 GL 线程(比如上面的 updateLogic)还是从 UI 线程(如上面的 onTouchEvent 方法)启动它有关系吗?

目前,我有一个 Activity 类的句柄,只需执行以下操作:

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

它工作正常(这是从 GL 线程完成的),但是,有些事情告诉我这不是完成此任务的正确方法。

【问题讨论】:

    标签: java android android-intent android-activity


    【解决方案1】:

    如果您的课程提供上下文、应用程序等,您可以轻松地这样做

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

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

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      相关资源
      最近更新 更多