首先,写一个类继承Appclication
import android.app.Application; import android.content.Context; import android.content.res.Resources; public class MyApplication extends Application{ private static MyApplication mcontext; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); mcontext=this; } public static Context getAppContext(){ return mcontext; } public static Resources getAppResources(){ return getAppResources(); } }
然后在manifest文件中的application中添加
android:name=".MyApplication"
这样,就能在整个application内使用MyApplication.getAppContext()来获得context而不用到处传递了