Disable Crashlytics/Answers based on user setting

主宰稳场 提交于 2019-12-11 11:32:23

问题


I am adding a new preference on my app, to allow the user to opt out of analytics reports. I am using in my app Crashlytics and Answers by Fabric.

I have this code within onCreate:

if (PreferenceHelper.getAllowAnalytics(context)) {
            Fabric.with(this, new Crashlytics());
        } else {
            //no crash or answers to be sent
        }
}

and each time I want to send an event I do it like this:

Answers.getInstance().logCustom(new CustomEvent("test event"));

This works well, when the user allows it. How should I handle the else when the user decides not to allow analytic?


回答1:


You could wrap all of your calls to Answers.getInstance in a new class that checks your preference and does nothing if it's not enabled? IE: Instead of Answers.getInstance().logCustom() WrappedAnswers.getInstance().logCustom(). And wrapped answers would do your if/else check in logCustom()




回答2:


Could do something like this without using an if/else:

CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(!PreferenceHelper.getAllowAnalytics(context)).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());

This way if your getAllowAnalytics method returns false, then Fabric/Crashlytics will be disabled, otherwise it will be enabled.



来源:https://stackoverflow.com/questions/31992076/disable-crashlytics-answers-based-on-user-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!