Android 获取静态上下文(Application)

在Android的清单文件做如下申明:

<application android:name="com.xyz.MyApplication"> 
</application> 
然后写这个类:

public class MyApplication extends Application{ 

    private static Context context; 

    public void onCreate(){ 
        super.onCreate(); 
        MyApplication.context = getApplicationContext(); 
    } 

    public static Context getAppContext() { 
        return MyApplication.context; 
    } 
} 
调用MyApplication.getAppContext()来静态的获取你应用程序的context。

 

posted @ 2018-09-13 15:28  HelloStudio  阅读(5896)  评论(0编辑  收藏  举报