从runnable接口访问活动上下文

时间:2022-02-15 17:32:52

I have this setup

我有这个设置

MainActivity Class creates BTHandler which is a utility class. It passes activity context and application context.

MainActivity类创建BTHandler,它是一个实用程序类。它传递活动上下文和应用程序上下文

public class MainActivity extends Activity implements View.OnClickListener{

    protected void onCreate(Bundle savedInstanceState) {
     ...
    currentBT = new BTHandle(this, MainActivity.this);
    }

    public Handler handler = new Handler() {
    @Override
    public void handleMessage(android.os.Message msg) {
        ...
        super.handleMessage(msg);
    }
};
} 

This is the Utiliy class. It uses both context for some computation and when is required it creates ConnectThread, to start a connection with a BT device. It passes the activity context so ConnectThread can send a message through the Handler.

这是Utiliy课程。它使用两个上下文进行某些计算,并在需要时创建ConnectThread,以启动与BT设备的连接。它传递活动上下文,以便ConnectThread可以通过Handler发送消息。

class BTHandle { 
    private final Context mainActivityContext;
    private final Activity mainActivity;
    ...
    public BTHandle(final Context context, final Activity activity){
        mainActivityContext = context;
        mainActivity = activity;
    }
    f(){
        ConnectThread connectAsClient = new ConnectThread(mainActivityContext, BTDevice, mBluetoothAdapter, BTUuid);
        new Thread(connectAsClient).start();
    }

}

And Finally here comes the error. This is ConnectThread. I want to send a message from here to MainActivity. But compiler says it cant resolve handler. So I'm guessing the context doesn't come trough it.

最后这里出现了错误。这是ConnectThread。我想从这里向MainActivity发送消息。但是编译器说它无法解析处理程序。所以我猜测上下文并没有通过它。

public class ConnectThread implements Runnable {
     private final Context mainActivity;
     public ConnectThread(Context context, BluetoothDevice device, BluetoothAdapter adapter, UUID BT_UUID) {
     ...
     mainActivity = context;
     }

     @Override
     public void run() {
         ...
       Message completeMessage = mainActivity.handler.obtainMessage(555, "CONNECTED");
       completeMessage.sendToTarget();
     }
}

How can I pass the context in the right way so ConnectThread can see handler?

如何以正确的方式传递上下文,以便ConnectThread可以看到处理程序?

2 个解决方案

#1


0  

You can either pass in the MainActivity object all the way down to ConnectRunnable i.e :

您可以将MainActivity对象一直传递给ConnectRunnable,即:

private final MainActivity mainActivity;
public ConnectThread(MainActivity context, BluetoothDevice device,
BluetoothAdapter adapter, UUID BT_UUID) {
 ...
 mainActivity = context;
 }

Or change your call to:

或者将您的电话改为:

 Message completeMessage = 
((MainActivity)(Activity)mainActivity)).handler.obtainMessage(555, "CONNECTED");

Also, your handler is bound to leak memory. Have a look at this for the fix: How to Leak a Context: Handlers & Inner Classes

此外,您的处理程序必然会泄漏内存。看看这个修复:如何泄漏上下文:处理程序和内部类

#2


0  

You can also define a public and static MainActvity variable and use it in your other class :

您还可以定义公共和静态MainActvity变量,并在其他类中使用它:

public class MainActivity extends Activity implements View.OnClickListener{
public static MainActivity mainActivity;
protected void onCreate(Bundle savedInstanceState) {
 ...
mainActivity = this;
currentBT = new BTHandle(this, MainActivity.this);
}

public Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
    ...
    super.handleMessage(msg);
}
};
} 

and in your other class :

在你的其他课堂上:

public class ConnectThread implements Runnable {
private MainActivity mainActivity;
 public ConnectThread(Context context, BluetoothDevice device, BluetoothAdapter adapter, UUID BT_UUID) {
 ...
 mainActivity = MainActivity.mainActivity;
 }

 @Override
 public void run() {
     ...
   Message completeMessage = mainActivity.handler.obtainMessage(555, "CONNECTED");
   completeMessage.sendToTarget();
 }
}

#1


0  

You can either pass in the MainActivity object all the way down to ConnectRunnable i.e :

您可以将MainActivity对象一直传递给ConnectRunnable,即:

private final MainActivity mainActivity;
public ConnectThread(MainActivity context, BluetoothDevice device,
BluetoothAdapter adapter, UUID BT_UUID) {
 ...
 mainActivity = context;
 }

Or change your call to:

或者将您的电话改为:

 Message completeMessage = 
((MainActivity)(Activity)mainActivity)).handler.obtainMessage(555, "CONNECTED");

Also, your handler is bound to leak memory. Have a look at this for the fix: How to Leak a Context: Handlers & Inner Classes

此外,您的处理程序必然会泄漏内存。看看这个修复:如何泄漏上下文:处理程序和内部类

#2


0  

You can also define a public and static MainActvity variable and use it in your other class :

您还可以定义公共和静态MainActvity变量,并在其他类中使用它:

public class MainActivity extends Activity implements View.OnClickListener{
public static MainActivity mainActivity;
protected void onCreate(Bundle savedInstanceState) {
 ...
mainActivity = this;
currentBT = new BTHandle(this, MainActivity.this);
}

public Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
    ...
    super.handleMessage(msg);
}
};
} 

and in your other class :

在你的其他课堂上:

public class ConnectThread implements Runnable {
private MainActivity mainActivity;
 public ConnectThread(Context context, BluetoothDevice device, BluetoothAdapter adapter, UUID BT_UUID) {
 ...
 mainActivity = MainActivity.mainActivity;
 }

 @Override
 public void run() {
     ...
   Message completeMessage = mainActivity.handler.obtainMessage(555, "CONNECTED");
   completeMessage.sendToTarget();
 }
}