【问题标题】:Android intent filter restart Activity?Android意图过滤器重启Activity?
【发布时间】:2016-07-09 11:34:12
【问题描述】:

我正在开发一个连接到平板电脑时与 USB 设备通信的应用程序。 为了避免用户接受 android 访问设备,我设置了一个意图过滤器:

<intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_device_filter" />

我的问题是,有了这个意图过滤器,每次插入设备并启动另一个活动时都会调用“onCreate”方法,如果没有意图过滤器,它只会被调用一次。 这是“onCreate”方法:

@Override   
protected void onCreate(Bundle savedInstanceState) {    
    try {   
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.activity_msp430_hid);               //define activity layout
        setVersionToTitle();    
        btnSend = (Button) findViewById(R.id.btnSend);      //Send button
        btnSend.setOnClickListener(this);                   //Listener for Send Button
        btnSend.setEnabled(true);
        btnSelectHIDDevice = (Button) findViewById(R.id.btnSelectHIDDevice);    //Select HID Device button
        btnSelectHIDDevice.setOnClickListener(this);        //Listener for Select HID Device button
        btnClear = (Button) findViewById(R.id.btnClear);    //Clear button
        btnClear.setOnClickListener(this);                  //Listener for Clear button
        edtxtHidInput = (EditText) findViewById(R.id.edtxtHidInput);    //User editable text area for sending information to attached device        
        log_txt = (EditText) findViewById(R.id.log_txt);    //Text area for displaying information      
        mLog("Initialized\nPlease select your USB HID device");         
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);    
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);  //Get USB permission intent for broadcast
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbReceiver, filter);             //Register broadcast receiver
        edtxtHidInput.setText("Enter Text Here");   
        uiHandler.postDelayed(runnable, 100);               //Start runnable after 100ms

    } catch (Exception e) {
        Log.e("Init", "Initialization error", e);

    }   
}

广播接收器:

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                setDevice(intent);
            }
        }
        //device attached
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            synchronized (this) {
                setDevice(intent);      //Connect to the selected device
            }
            if (device == null) {
                mLog("device connected");
            }
        }
        //device detached
        if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            if (device != null) {
                device = null;
                //btnSend.setEnabled(false);
            }
            mLog("device disconnected");
        }
    }

简单连接设备的“setDevice”方法:

private void setDevice(Intent intent) {
    device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    if (device != null && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
        mLog("Selected device VID:" + Integer.toHexString(device.getVendorId()) + " PID:" + Integer.toHexString(device.getProductId()));
        connection = mUsbManager.openDevice(device);        //Connect to device
        intf = device.getInterface(0);

为什么?

【问题讨论】:

  • 回答我自己,Android 将连接到 USB 设备视为配置更改,它会再次启动活动,对吗?

标签: android usb intentfilter


【解决方案1】:

显然,这是默认行为。检查android:launchModehere

默认的launchMode是standard,这意味着每次连接USB设备时,都会创建一个新的Activity实例。如果您设置了singleTop,则调用onNewIntent() 并重新使用您的活动(如果它位于堆栈顶部)。否则它会创建一个新的。

您也可以使用singleTasksingleInstance,但在大多数情况下,Google 不鼓励这样做。但有时它可能是解决问题的正确方法。

【讨论】:

    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多