首先,寫一個類繼承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而不用到處傳遞了