1

我有一个意图服务,可以将数据从手机上传到我的服务器。只要网络连接可用,我就从广播接收器启动它。一切正常,但是当我通过 Android 4.0.3 模拟器对其进行测试时,它给出了严格模式错误 android.os.NetworkOnMainThreadException。就像这个网络作业在主线程上一样,但据我所知,intentservice 已经创建了一个工作线程。我也发现了这个链接http://code.google.com/p/android/issues/detail?id=23495说将网络作业移动到后台线程。工作线程不是后台线程吗?

编辑:这是我的广播接收器。

public void onReceive(Context context, Intent intent) {
    final String UploadingItems = "paUploadingItems";
    final String preferencesFileName = "paSettings";

    SharedPreferences sharedPref;   
    String fileQueque;

    if(isNetworkAvailable(context)){        

        sharedPref = context.getSharedPreferences(preferencesFileName,0);
        fileQueque = sharedPref.getString(UploadingItems, "");
        Log.d(TAG, "quque"+fileQueque);
        if(fileQueque.length() > 0){

                Intent intentUpdater = new Intent(context, updaterService.class);               
                intentUpdater.putExtra("processMode", 6);
                intentUpdater.putExtra("fileq", fileQueque.toString());
                context.startService(intentUpdater);

            }
    }       

}

private boolean isNetworkAvailable(Context context){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();     
    Log.d(TAG, activeNetworkInfo.getTypeName());
    return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected());       

}

这是我在 LogCat 中看到的错误:

04-17 15:28:58.114: E/AndroidRuntime(574): FATAL EXCEPTION: main
04-17 15:28:58.114: E/AndroidRuntime(574): java.lang.RuntimeException: Unable to start     receiver org.test.dairy.OnNetworkReceiver: android.os.NetworkOnMainThreadException
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.os.Looper.loop(Looper.java:137)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-17 15:28:58.114: E/AndroidRuntime(574):  at java.lang.reflect.Method.invokeNative(Native Method)
04-17 15:28:58.114: E/AndroidRuntime(574):  at java.lang.reflect.Method.invoke(Method.java:511)
04-17 15:28:58.114: E/AndroidRuntime(574):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-17 15:28:58.114: E/AndroidRuntime(574):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-17 15:28:58.114: E/AndroidRuntime(574):  at dalvik.system.NativeStart.main(Native Method)
04-17 15:28:58.114: E/AndroidRuntime(574): Caused by: android.os.NetworkOnMainThreadException
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-17 15:28:58.114: E/AndroidRuntime(574):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-17 15:28:58.114: E/AndroidRuntime(574):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-17 15:28:58.114: E/AndroidRuntime(574):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
04-17 15:28:58.114: E/AndroidRuntime(574):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
04-17 15:28:58.114: E/AndroidRuntime(574):  at org.test.dairy.OnNetworkReceiver.transferData(OnNetworkReceiver.java:101)
04-17 15:28:58.114: E/AndroidRuntime(574):  at   org.test.dairy.OnNetworkReceiver.onReceive(OnNetworkReceiver.java:57)
04-17 15:28:58.114: E/AndroidRuntime(574):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
04-17 15:28:58.114: E/AndroidRuntime(574):  ... 10 more
4

2 回答 2

2

您的 isNetworkAvailable() 方法检查主 UI 线程上的网络 I/O,并且 Android 认为该阻塞。我只会触发对 IntentService 的请求,而不检查 Activity 中的连接性,让 IntentService 抛出异常并在无法建立连接时向 Activity 报告。

于 2012-04-17T17:17:42.990 回答
0

将代码移动到 IntentService。广播接收器的 onReceive 方法在 UI 线程上运行,网络代码导致它崩溃。只需将意图重定向到 IntentService 就可以了。

这也使代码更简单。接收器并不真正意味着执行复杂的逻辑,它们将消息从系统重定向到您的应用程序。服务意味着在后台做繁重的工作。

于 2012-04-17T17:21:42.990 回答