1. 程式人生 > >在Android專案中,隨處獲取Context的工具類

在Android專案中,隨處獲取Context的工具類

工具類

import android.app.Application;

public class ContextUtil extends Application {
    private static ContextUtil instance;

    public static ContextUtil getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        instance = this;
    }
}


呼叫

Context c = ContextUtil.getInstance();