创建方法
public Helper(Context context)
{
this.context = context;
}
-------------分割线--------------
在工具类里直接获取applicationContext,这样可以避免每次调用都传入context
先自己定义一个application
public class MyApplication extends Application {
private static Context mContext;
@Override
public void onCreate(){
super.onCreate();
mContext = getApplicationContext();
}
public static Context getGlobalContext(){
return mContext;
}
}
然后在manifest中使用它
android:name="包名路径.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
工具类中这样使用就可以了
private NetworkHelper(){
context = MyApplication.getGlobalContext();
}