2

我设置了严格模式,然后设置了 Bugsense 处理程序。这导致违反 StrictMode 策略。

为什么会发生这种情况有什么合乎逻辑的解释吗?我怎样才能摆脱它?

我的应用程序.java:

    android.os.StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new android.os.StrictMode.ThreadPolicy.Builder();
    android.os.StrictMode.VmPolicy.Builder vmPolicyBuilder = new android.os.StrictMode.VmPolicy.Builder();
    android.os.StrictMode.setThreadPolicy(threadPolicyBuilder.detectAll().penaltyLog().build());
    android.os.StrictMode.setVmPolicy(vmPolicyBuilder.detectAll().penaltyLog().build());

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {

                BugSenseHandler.initAndStartSession(MyApplication.this, "my-id");

            return null;
        }
    }.execute();

堆栈:

01-21 09:43:40.889: D/StrictMode(29660): StrictMode policy violation: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
01-21 09:43:40.889: D/StrictMode(29660):    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
01-21 09:43:40.889: D/StrictMode(29660):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:228)
01-21 09:43:40.889: D/StrictMode(29660):    at java.io.FileInputStream.<init>(FileInputStream.java:80)
01-21 09:43:40.889: D/StrictMode(29660):    at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:381)
01-21 09:43:40.889: D/StrictMode(29660):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
01-21 09:43:40.889: D/StrictMode(29660):    at com.bugsense.trace.BugSenseHandler.initAndStartSession(Unknown Source)
01-21 09:43:40.889: D/StrictMode(29660):    at com.bugsense.trace.BugSenseHandler.initAndStartSession(Unknown Source)
01-21 09:43:40.889: D/StrictMode(29660):    at my.app$1.doInBackground(MyApplication.java:52)
01-21 09:43:40.889: D/StrictMode(29660):    at my.app$1.doInBackground(MyApplication.java:1)
01-21 09:43:40.889: D/StrictMode(29660):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-21 09:43:40.889: D/StrictMode(29660):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-21 09:43:40.889: D/StrictMode(29660):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-21 09:43:40.889: D/StrictMode(29660):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-21 09:43:40.889: D/StrictMode(29660):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-21 09:43:40.889: D/StrictMode(29660):    at java.lang.Thread.run(Thread.java:1019)
4

3 回答 3

2

您收到此错误是因为插件从主线程中的 SharedPreferences 读取信息。

不幸的是,目前没有办法克服这一点,除了降低严格模式的政策。

我们正在努力解决这个问题,即将发布的下一个版本的 BugSense 插件将在严格模式下工作得很好。

感谢您的反馈,它非常有价值,如果您有任何其他问题,我很乐意在我们的支持论坛上回答!

于 2013-01-22T11:34:27.360 回答
0

您是否尝试过新版本的 BugSense SDK (3.2)?他们修复了严格模式问题。

在此处查看更改日志。

于 2013-01-29T09:47:58.397 回答
-2

我有类似的问题。我为我的应用创建了 DEVELOPER_MODE。这只是一个布尔标志,它告诉应用程序我是否处于开发人员模式。我只在开发者模式下启用严格模式,没有理由为已发布的应用启用严格模式。另一方面,我禁用了开发人员模式的 bugsense,因为如果我在 LogCat 中看到它们,我不需要报告错误。我的代码看起来像这样

public static final boolean DEVELOPER_MODE = true;


@Override
public void onCreate() {

    if (DEVELOPER_MODE) {
    StrictMode.setThreadPolicy(new  StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
    StrictMode.setVmPolicy(new  StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());

    }else{
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {

                BugSenseHandler.initAndStartSession(MyApplication.this, "my-id");

            return null;
            }
        }.execute();
    }
}
于 2013-01-24T09:15:55.163 回答