开始从静态方法的机器人活动 [英] Starting an Android Activity from a static method

查看:92
本文介绍了开始从静态方法的机器人活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始从静态Java方法的活动在Android设备上。 我没有作为参数传递给静态函数的任何内容或任何东西。 为了启动活动我必须调用startActivity与当前正在运行的方法这个指针。那么,有没有一种方式来获得当前正在运行的活动呢?

I want to start an activity from a static java method on an android device. I do not have any context or anything passed as parameter to the static function. For starting the activity I must call "startActivity" with the current running method as "this" pointer. So is there a way to get the current running activity?

推荐答案

创建一个类在你的应用程序扩展类的应用,定义了一个静态的背景和与您的应用程序上下文初始化此。你可以从这个类公开访问定义的静态参考的静态方法。这就是它。

Create a Class in your app extending class Application, define a static context and initialize this with your application context. You can expose a static method from this class for accessing defined static reference. Thats it.

class MyApp extends Application{

    private static Context mContext;

    public void onCreate(){
       mContext = this.getApplicationContext();
    }

    public static Context getAppContext(){
       return mContext;
    }
}

现在你可以使用这个静态方法在乌拉圭回合的应用程序的任何地方accesing环境。

now you can use this static method for accesing context anywhere in ur app.

这篇关于开始从静态方法的机器人活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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