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

Android 在不同线程中声明广播接收器时出错,android,multithreading,Android,Multithreading,我正在尝试将广播接收器加载到另一个线程中,而不是我的UI线程中,因为它会减慢我的应用程序。以下是我的广播接收器代码: private BroadcastReceiver battery_receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { boolean isPresent = intent.getBooleanE

我正在尝试将广播接收器加载到另一个线程中,而不是我的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)
因为它减慢了我的应用程序

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

但当我运行它时,我在logcat上运行应用程序并出现此错误几秒钟后得到错误

错误消息是不言自明的:

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

您不能像现在这样从后台线程修改UI。删除“<代码> HooLeRead < /Cord>.< /P> @ Soheyl:您可能会考虑问一个单独的堆栈溢出问题,在那里您提供了性能问题及其位置的证据。下面,解释一下你是如何确定你有问题的,以及这段代码是问题的根源。接收方应该成为性能问题根源的唯一方式是调用它的频率太高<代码>动作\u电池\u更换不应该被如此频繁地调用,尽管可能会有错误的设备和自定义ROM播放太多。