在不同的线程中声明广播接收器时出错

时间:2016-11-23 18:29:14

标签: android multithreading

我正在尝试在不同的线程而不是我的UI线程中加载广播接收器,因为我的应用程序速度减慢,这是我的广播接收器代码:

private BroadcastReceiver battery_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isPresent = intent.getBooleanExtra("present", false);
        //String technology = intent.getStringExtra("technology");
        //int plugged = intent.getIntExtra("plugged", -1);
        //int scale = intent.getIntExtra("scale", -1);
        int health = intent.getIntExtra("health", 0);
        //int status = intent.getIntExtra("status", 0);
        //int rawlevel = intent.getIntExtra("level", -1);
        float voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
        if (voltage > 1000)
            voltage=voltage / 1000f;
        //int voltage = intent.getIntExtra("voltage", 0);
        float temperature = intent.getIntExtra("temperature", 0);
        if (temperature > 100) {
            temperature = temperature / 10f;
        }

        //int level = 0;
        Bundle bundle = intent.getExtras();
        Log.i("BatteryLevel", bundle.toString());
        if (isPresent) {

            //String info = "Battery Level: " + level + "%\n";
            //info += ("Technology: " + technology + "\n");
            //info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
            //info += ("Health: " + getHealthString(health) + "\n");
            //info += ("Status: " + getStatusString(status) + "\n");
            //info += ("Voltage: " + voltage + "\n");
            //info += ("Temperature: " + temperature + "\n");
            setBatteryLevelText(String.valueOf(voltage) + " V");
            setBatteryLevelText2(String.valueOf(temperature) + " °C");
            setBatteryLevelText3(getHealthString(health));
        } else {
            setBatteryLevelText("Battery not present!!!");
        }
    }
};

以下是将其加载到线程上的代码

private void registerBatteryLevelReceiver() {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Handler handler; // Handler for the separate Thread

    HandlerThread handlerThread = new HandlerThread("MyNewThread");
    handlerThread.start();
    Looper looper = handlerThread.getLooper();
    handler = new Handler(looper);
    getApplicationContext().registerReceiver(battery_receiver, filter, null, handler);
//registerReceiver(battery_receiver, filter);
}

并且所有这些都在oncreate方法之前,但是当我运行它时,我在logcat运行应用程序后出现此错误几秒钟后出现错误:

FATAL EXCEPTION: MyNewThread
                                                                 Process: com.soheil.prolightfa, PID: 8880
                                                                 java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 bqHint=4 (has extras) } in com.soheil.prolightfa.MainActivity$1@fce2659
                                                                     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1003)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:158)
                                                                     at android.os.HandlerThread.run(HandlerThread.java:61)
                                                                  Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
                                                                     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8128)
                                                                     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1220)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.widget.TableLayout.requestLayout(TableLayout.java:227)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.view.View.requestLayout(View.java:20085)
                                                                     at android.widget.TextView.setTypeface(TextView.java:3316)
                                                                     at com.soheil.prolightfa.MainActivity.setBatteryLevelText(MainActivity.java:132)
                                                                     at com.soheil.prolightfa.MainActivity.access$000(MainActivity.java:57)
                                                                     at com.soheil.prolightfa.MainActivity$1.onReceive(MainActivity.java:101)
                                                                     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:993)

1 个答案:

答案 0 :(得分:0)

  

导致我的应用放慢

这是不太可能的。阅读一些额外内容并更新一些TextView小部件不会花费太多时间。

  

但是当我运行它时,我在logcat

上运行此错误后几秒钟后收到错误

错误消息相当不言自明:

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

您无法像在操作那样从后台线程修改UI。删除HandlerThread