异步任务问题Android

时间:2014-12-18 10:20:10

标签: java android bluetooth

异步任务实现在基于蓝牙的Android应用程序中提供以下异常。

异步任务代码

package com.example.vedangbluetoothlock;

import java.util.Arrays;

import android.os.AsyncTask;

public class readRegAsyncTask extends AsyncTask <readRegHolding,Void,readRegHolding> {
    @Override
    protected readRegHolding doInBackground(readRegHolding...params) {
        // TODO Auto-generated method stub
        int uid = params[0].uid;
        int num_of_reg = params[0].num_of_reg;
        int start_address = params[0].start_address;

        blueconnect.readHoldingReg(uid, params[0].data, start_address, num_of_reg);
        while(!blueconnect.macThread);

        //params[0].data = Arrays.copyOf(blueconnect.res, num_of_reg);

        return params[0];
    }

    @Override
    protected void onPostExecute(readRegHolding result) {
        // TODO Auto-generated method stub
        // super.onPostExecute(result);
        for (int i = 0; i<result.num_of_reg;i++) {
            result.data[i] = blueconnect.res.getRegisterValue(i);
        }
    }

}

EXCEPTION

12-18 15:41:18.634: E/ActivityThread(23724): Activity com.example.vedangbluetoothlock.Bluetooth has leaked IntentReceiver com.example.vedangbluetoothlock.Bluetooth$1@42649300 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-18 15:41:18.634: E/ActivityThread(23724): android.app.IntentReceiverLeaked: Activity com.example.vedangbluetoothlock.Bluetooth has leaked IntentReceiver com.example.vedangbluetoothlock.Bluetooth$1@42649300 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:803)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:604)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1494)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ContextImpl.registerReceiver(ContextImpl.java:1474)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ContextImpl.registerReceiver(ContextImpl.java:1468)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467)
12-18 15:41:18.634: E/ActivityThread(23724):       at com.example.vedangbluetoothlock.Bluetooth.onCreate(Bluetooth.java:90)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.Activity.performCreate(Activity.java:5231)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.os.Handler.dispatchMessage(Handler.java:102)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.os.Looper.loop(Looper.java:136)
12-18 15:41:18.634: E/ActivityThread(23724):       at android.app.ActivityThread.main(ActivityThread.java:5146)
12-18 15:41:18.634: E/ActivityThread(23724):       at java.lang.reflect.Method.invokeNative(Native Method)
12-18 15:41:18.634: E/ActivityThread(23724):       at java.lang.reflect.Method.invoke(Method.java:515)
12-18 15:41:18.634: E/ActivityThread(23724):       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
12-18 15:41:18.634: E/ActivityThread(23724):       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
12-18 15:41:18.634: E/ActivityThread(23724):       at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您需要在停止活动时取消注册接收器:

@Override
protected void onDestroy() {
   super.onDestroy();
   unregisterReceiver(yourReceiver);

}