0

在创建这种基本方法来检查设备的网络带宽时,我真的被 logcat 堆栈跟踪(我不太熟悉 android)难住了。该代码加载网页,计算所需时间,并测量下载的内容。然后将该值除以加载网页所需的毫秒数,以计算近似带宽。

代码:

public class MainActivity extends Activity {
private int linkSpeed;
private TextView textView;

/** Called when the activity is created. */

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    textView.setTextSize(25);

  //Download your image
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                String urlString = "http://www.google.com";
                long startTime = System.currentTimeMillis();
                HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
                HttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
                long endTime = System.currentTimeMillis();

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity bufHttpEntity;
                bufHttpEntity = new BufferedHttpEntity(entity);

                //You can re-check the size of your file
                final long contentLength = bufHttpEntity.getContentLength();

                // Log
                String TAG = "tag";
                Log.d(TAG, "[BENCHMARK] Dowload time :"+(endTime-startTime)+" ms");

                // Bandwidth : size(KB)/time(s)
                float bandwidth = contentLength / ((endTime-startTime) *1000);

                textView.setText("bandwidth = " + bandwidth);
                setContentView(textView);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

我对 LOGCAT 抱怨无法创建处理程序感到困惑,因为我从未在此类中使用过。LOGCAT 跟踪:

07-03 14:42:18.214: D/dalvikvm(2401): Late-enabling CheckJNI
07-03 14:42:18.474: D/tag(2401): [BENCHMARK] Dowload time :166 ms
07-03 14:42:18.474: W/dalvikvm(2401): threadid=11: thread exiting with uncaught exception (group=0x4162e930)
07-03 14:42:18.474: E/AndroidRuntime(2401): FATAL EXCEPTION: Thread-201
07-03 14:42:18.474: E/AndroidRuntime(2401): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.os.Handler.<init>(Handler.java:197)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.os.Handler.<init>(Handler.java:111)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at com.android.internal.app.ActionBarImpl.<init>(ActionBarImpl.java:108)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.app.Activity.initActionBar(Activity.java:1867)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at android.app.Activity.setContentView(Activity.java:1902)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at com.example.networkinfo.MainActivity$1.run(MainActivity.java:69)
07-03 14:42:18.474: E/AndroidRuntime(2401):     at java.lang.Thread.run(Thread.java:856)
07-03 14:42:18.524: D/dalvikvm(2401): GC_CONCURRENT freed 232K, 5% free 7525K/7912K, paused 9ms+2ms, total 50ms
07-03 14:42:18.894: D/libEGL(2401): loaded /system/lib/egl/libEGL_tegra.so
07-03 14:42:18.914: D/libEGL(2401): loaded /system/lib/egl/libGLESv1_CM_tegra.so
07-03 14:42:18.924: D/libEGL(2401): loaded /system/lib/egl/libGLESv2_tegra.so
07-03 14:42:18.944: D/OpenGLRenderer(2401): Enabling debug mode 0
4

3 回答 3

0

您不能从非 UI 线程调用 setContentView。我的建议是将该线程转换为 AsyncTask 并在 onPostExecute 中设置内容视图。您的 setText 调用中也存在同样的问题。

于 2013-07-03T19:53:31.910 回答
0

这条线

textView.setText("bandwidth = " + bandwidth);

给你添麻烦了。您正在尝试UI在背景上更新Thread。您需要使用其他方法将其设为AsyncTask并更新UIdoInBackground()runOnUiThread()

这是使用 runOnUiThread() 的示例

和 AsyncTask 之一

AsyncTask 文档

于 2013-07-03T19:53:34.910 回答
0
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

您不能setContentView()从后台线程调用。此外,您需要在 Activity 启动时显示一些UI,而不是等待可能永远不会成功完成的 HTTP 操作。

相反,在做后台工作setContentView() 之前打电话。特别是,考虑使用 aAsyncTask而不是 a Thread,并使用onPostExecute().

于 2013-07-03T19:54:34.080 回答