2016-03-22 15 views
1

私は、タブレットに接続したときにUSBデバイスと通信するアプリを開発しています。 は、デバイスにアクセスするには、Android受け入れるようにユーザーを避けるために、私はインテントフィルタを設定します。Androidのインテントフィルタの再起動アクティビティ?

私の質問は、このインテントフィルタを持つ、「のonCreate」メソッドは、私は、デバイスと別のものを接続するたびに呼び出されますされ
<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" /> 

アクティビティが開始され、インテントフィルタなしでは1回だけ呼び出されます。

@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); 

なぜここ は "のonCreate" 方法は?

+0

自分自身に答えるために、Androidは設定変更としてUSBデバイスへの接続を認識し、再びアクティビティを開始します。 – Romain

答えて

0

明らかに、これがデフォルトの動作です。 android:launchModehereを確認してください。

デフォルトのlaunchModeはstandardです。つまり、USBデバイスを接続するたびに新しいアクティビティインスタンスが作成されます。 singleTopを設定した場合は、onNewIntent()が呼び出され、スタックの最上部にあるアクティビティが再利用されます。それ以外の場合は、新しいものが作成されます。

また、singleTaskまたはsingleInstanceを使用することもできますが、ほとんどの場合、Googleはこれを嫌うものです。しかし時にはそれが問題の正しい解決策になることもあります。

関連する問題