Sonar扫描会报:
Instance methods should not write to "static" fields Correctly updating a static field from a non-static method is tricky to get right and could easily lead to bugs if there are multiple class instances and/or multiple threads in play. Ideally, static fields are only updated from synchronized static methods. This rule raises an issue each time a static field is updated from a non-static method.
翻译:
正确地从非静态方法更新静态字段是很棘手的, 如果在运行中有多个类实例和 / 或多个线程, 则可以很容易地导致 bug。理想情况下, 静态字段只从同步静态方法更新。 每次从非静态方法更新静态字段时, 此规则将引发一个问题。
修改方法:将非静态方法中对静态方法赋值的语句,单独封装一个静态方法,并且加上synchronized关键字,这样就不会导致多线程去修改这个字段导致其他的问题了。
private static FotaApplication mInstance;
public static synchronized void setContext(FotaApplication context){
mInstance = context;
}
public void onCreate() {
setContext(this)
}