如何使用从另一个活动或主类的活动中调用处理程序

时间:2017-08-09 08:53:05

标签: java android inputstream handler outputstream

我正在尝试使用输入输出流与Android上的USB通信,因为我在一个活动中使用处理程序,它工作正常,但如果在每个活动中分别使用处理程序它不能正常工作,所以我希望作为所有活动的调解者,在活动中进行共同活动并进行交流,我尝试了类似

的活动
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();
    }
}

3 个答案:

答案 0 :(得分:1)

在这种情况下,我们使用IntentService。定义IntentService的位置,该类是在收到操作时将在后台运行的类。然后你可以处理输入输出流(在后台!!这就是我们想要的),然后使用BroadcastReceiver向你的活动发送广播。

答案 1 :(得分:1)

您正在使用类似普通java类的活动类,我看到第二个活动没有任何视图文件。您可以使用构造函数创建一个名为UsbCommunication的普通类,并在您的活动中对其进行初始化,如下所示:

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();
}

在您的活动中调用函数:

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;
    }
}

答案 2 :(得分:1)

您可以在Application类中创建一个Handler。

public class YourApp extends Application {

    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler(Looper.getMainLooper());
    }

    public Handler getHandler() {
        return handler;
    }
}

然后你可以从其他组件中获取一个处理程序,例如来自Activity:

((YourApp)getApplication()).getHandler();

不要忘记在清单文件中设置应用程序名称。

<application
    android:name=".YourApp"
....>