开始从静态方法的机器人活动静态、机器人、方法

2023-09-12 23:44:11 作者:俺媳妇长得丑不会听你乱说

我要开始从静态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.