gpt4 book ai didi

java - 如何在另一个 Activity 或主类的 Activity 中调用处理程序

转载 作者:行者123 更新时间:2023-12-01 20:17:00 26 4
gpt4 key购买 nike

我正在尝试使用输入输出流与android上的USB进行通信,因为我在一个 Activity 中使用处理程序,它工作正常,但如果在每个 Activity 中单独使用处理程序,它就无法正常工作,所以我希望一个共同的 Activity 并在 Activity 内进行通信作为所有 Activity 的中介,我尝试了类似的东西

public class BasicAccessoryDemo extends Activity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mycontrol, close_command;
mycontrol = (Button) findViewById(R.id.send_command);
mycontrol.setOnClickListener(this);
}

@Override
public void onStart() {
super.onStart();
}

@Override
public void onResume() {
super.onResume();
}

public void onClick(View view) {
switch (view.getId()) {
case R.id.send_command:
byte[] commandPacket = new byte[2];
commandPacket[0] =0x12;
commandPacket[1] =0x34;
Usb_Communciation.Send_message(commandPacket);
break;
}
}

}

另一个保存处理程序的类是

public class Usb_Communciation extends Activity{
public final static int USBAccessoryWhat = 0;
public int firmwareProtocol = 0;
public static USBAccessoryManager accessoryManager;
public static String TAG = "MICROCHIP";
public static final int APP_CONNECT = (int)0xAE;
public boolean deviceAttached = false;

public void onCreate(Bundle savedInstanceState) {
accessoryManager = new USBAccessoryManager(handler, USBAccessoryWhat);
//accessoryManager.enable(this, getIntent());
}

public static void Send_message(byte[] data) {
try{
accessoryManager.write(data);
}catch (Exception e){
Log.d(TAG,
"USBAccessoryManager:write():IOException: arasu "
+ e.toString());
e.printStackTrace();
}
}

public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] commandPacket = new byte[64];
byte[] WriteValue = new byte[2];

switch(msg.what)
{
case USBAccessoryWhat:
boolean StopReading = true;
int count = 0;
switch(((USBAccessoryManagerMessage)msg.obj).type)
{
case READ:
if(accessoryManager.isConnected() == false) {
return;
}
while(true) {
if (accessoryManager.available() < 2) {
break;
}
}
break;
case READY:
String version = ((USBAccessoryManagerMessage)msg.obj).accessory.getVersion();
firmwareProtocol = getFirmwareProtocol(version);
switch(firmwareProtocol){
case 1:
deviceAttached = true;
break;
case 2:
deviceAttached = true;
commandPacket[0] = (byte) APP_CONNECT;
commandPacket[1] = 0;
accessoryManager.write(commandPacket);
Log.d(TAG,"connect message sent.");
break;
}
break;
}
break;
} //switch
} //handleMessage
}; //handler

public int getFirmwareProtocol(String version) {
String major = "0";
int positionOfDot;
positionOfDot = version.indexOf('.');
if(positionOfDot != -1) {
major = version.substring(0, positionOfDot);
}
return new Integer(major).intValue();
}
}

最佳答案

您正在使用像普通java类一样的 Activity 类,我发现第二个 Activity 没有任何 View 文件。您可以使用构造函数创建一个名为 UsbCommunication 的普通类,并在您的 Activity 中初始化它,如下所示:

public class Usb_Communciation {
public final static int USBAccessoryWhat = 0;
public int firmwareProtocol = 0;
public static USBAccessoryManager accessoryManager;
public static String TAG = "MICROCHIP";
public static final int APP_CONNECT = (int)0xAE;
public boolean deviceAttached = false;


public Usb_Communciation (/*Pass neccesary parameters here from activity*/) {
accessoryManager = new USBAccessoryManager(handler, USBAccessoryWhat);
}


public void Send_message(byte[] data) {
try{
accessoryManager.write(data);
}catch (Exception e){
Log.d(TAG,
"USBAccessoryManager:write():IOException: arasu "
+ e.toString());
e.printStackTrace();
}
}

public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] commandPacket = new byte[64];
byte[] WriteValue = new byte[2];

switch(msg.what)
{
case USBAccessoryWhat:
boolean StopReading = true;
int count = 0;
switch(((USBAccessoryManagerMessage)msg.obj).type)
{
case READ:
if(accessoryManager.isConnected() == false) {
return;
}
while(true) {
if (accessoryManager.available() < 2) {
break;
}
}
break;
case READY:
String version = ((USBAccessoryManagerMessage)msg.obj).accessory.getVersion();
firmwareProtocol = getFirmwareProtocol(version);
switch(firmwareProtocol){
case 1:
deviceAttached = true;
break;
case 2:
deviceAttached = true;
commandPacket[0] = (byte) APP_CONNECT;
commandPacket[1] = 0;
accessoryManager.write(commandPacket);
Log.d(TAG,"connect message sent.");
break;
}
break;
}
break;
} //switch
} //handleMessage
}; //handler

public int getFirmwareProtocol(String version) {
String major = "0";
int positionOfDot;
positionOfDot = version.indexOf('.');
if(positionOfDot != -1) {
major = version.substring(0, positionOfDot);
}
return new Integer(major).intValue();
}

并在您的 Activity 中调用该函数:

public void onClick(View view) {
switch (view.getId()) {
case R.id.send_command:
byte[] commandPacket = new byte[2];
commandPacket[0] =0x12;
commandPacket[1] =0x34;
Usb_Communication usbCom = new Usb_Communication();
usbCom.Send_message(commandPacket);
break;
}
}

关于java - 如何在另一个 Activity 或主类的 Activity 中调用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45585904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com