android 静态类引用上下文内存泄漏,android – 内存泄漏,用于静态声明上下文和INSTANCE,如何更改它?...

目前在我的代码库中我有以下类(部分内容),它显示了2个内存泄漏,消息“不要将

Android上下文类放在静态字段中(静态引用Myclass,其中字段上下文指向Context);这是内存泄漏(也打破了Instant Run)“

我不确定替代方案是什么.这是100%的内存泄漏吗?我在“INSTANCE”上收到泄漏警告和上下文的“静态”声明.知道怎么去修理吗?

public enum Myclass {

INSTANCE;

public static final boolean TLS_ENABLED = true;

private static final String TAG = Myclass.class.getSimpleName();

private static final String SP = "My_class";

private static Context context;

public void init(Context context, String appKey, String appSecret) {

init(context, null, appKey, appSecret);

}

/**

* Initialize class

*

* @param context Application level context.

* @param apiUrl API url of backend server

* @param appKey Application key

* @param appSecret Application secret

* @throws IllegalArgumentException If activity instance will be passed as the context

* @throws IllegalArgumentException If application key is empty or null

* @throws IllegalArgumentException If application secret is empty or null

*/

public void init(Context context, String apiUrl, String appKey, String appSecret) {

if (null == context) { throw new NullPointerException(); }

if (!(context instanceof Application)) { throw new IllegalArgumentException("Supply my class with application context"); }

// if (TextUtils.isEmpty(apiUrl)) { throw new IllegalArgumentException("Api url can't be null or empty string"); }

if (TextUtils.isEmpty(appKey)) { throw new IllegalArgumentException("App key can't be null or empty string"); }

if (TextUtils.isEmpty(appSecret)) { throw new IllegalArgumentException("App secret can't be null or empty string"); }

this.apiUrl = apiUrl;

this.appKey = appKey;

this.appSecret = appSecret;

this.sp = context.getSharedPreferences(SP, Context.MODE_PRIVATE);

MyClass.context = context;

initManagers();

}

/**

* Initializes managers. This method must be called after constructor

* returns, as the managers during own initialization may use myclass.get()

* method.

*/

private void initManagers() {

accountManager = new AccountManager();

myclassApi = new MyclassApi(context, apiUrl);

contactManager = new ContactManager();

connectionManager = new ConnectionManager();

meetingListManager = new MeetingListManager();

}

/**

* Returns {@link Context} that was passed to

* {@link myclass#init(Context, String, String)}.

*

* @return

*/

public static Context getContext() {

return context;

}

/**

* Returns {@link SharedPreferences} instance.

*

* @return SharedPreferences

*/

public SharedPreferences getSp() {

return this.sp;

}

public static class Event {

private State state = State.SUCCESS;

private Throwable t;

private T data;

private String errorMessage;

/**

* Event state. If event related to network request/response

* operations - state indicates the physical (not logical)

* success or fail of request.

*/

public enum State {

/**

* Indicates that attempt to get data or perform task successful

*/

SUCCESS,

/**

* Indicates that attempt to get data or perform task fails,

* and reason of fail is the incorrect request data

*/

FAIL,

/**

* Indicates that attempt to get data or perform task encounter an error

* mostly due to connection problem

*/

ERROR,

/**

* Indicates that attempt to get data or perform task was ignored

* according to internal state of event producer.

*/

IGNORED

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值