2014-04-02 1 views
0

내 앱에는 두 개의 서로 다른 앱 (홈 화면에 별도의 아이콘)이 표시되는 두 개의 activities이 있습니다. 이제는 둘 다 USB 장치가 연결되어 있는지를 청취하려는 의도 필터가 있습니다. 문제는 Activity A에 있고 USB 장치를 태블릿에 넣으면 자동으로 Activity B이 시작된다는 것입니다. 내가 달성하고자하는 것은 : 다른 활동에 대한 동일한 인 텐트 필터 처리

Activity A -> insert USB -> stay in Activity A and do something with the USB 
Activity B -> insert USB -> stay in Activity B and do something else with the USB 

여기 나의

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="mypackage.myapp" 
    android:versionCode="100" 
    android:versionName="1.0.0-dev" > 

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="19" /> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/my_icon" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="mypackage.ActivityA" 
      android:icon="@drawable/my_icon" 
      android:label="@string/ActivityA" 
      android:launchMode="singleTask" 
      android:screenOrientation="landscape" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 

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

     </activity> 
     <activity 
      android:name="mypackage.ActivityB" 
      android:icon="@drawable/another_icon" 
      android:label="@string/ActivityB" 
      android:launchMode="singleTask" 
      android:screenOrientation="landscape" > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
      </intent-filter> 
      <meta-data 
       android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
       android:resource="@xml/lib_device_filter" /> 

     </activity> 
    </application> 
</manifest> 
+0

해결 했습니까? – jayeffkay

+0

예 및 아니오 : 요구 사항이 변경되어 두 앱이 하나의 단일 앱에 결합되었습니다. – JohnPlata

답변

2

매니페스트 내가 프로젝트에서 같은 문제를 발견하고 내가 당신과 함께 구현 솔루션을 공유하고자합니다.

우선, 나는 DumpActivity라고 불리는 활동을했습니다. 이 작업은 인 텐트 필터를 구현하기 만하면 USB가 연결되었을 때들을 수 있습니다. 활동이 시작되면 즉시 이전 활동으로 돌아갑니다. 이 시점에서 이전 활동 (예에서 활동 A 또는 B)이 USB와 상호 작용할 수 있습니다.

/** 
* This activity have the intent filters of the USB (see AndroidManifest.xml). 
* 
* When it is the first time that the USB microphones are plugged, this activity is launched. 
* This is a solution to disable the popup dialog when a USB device is connected. 
* 
* When this activity is launched, immediately it is destroyed to return to the previous 
* activity 
*/ 

public class DumpActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (getIntent().getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { 
      finish(); 
     } 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { 
      finish(); 
     } 
    } 
} 

그런 다음, 매니페스트에 :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="mypackage.myapp" 
    android:versionCode="100" 
    android:versionName="1.0.0-dev" > 

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="19" /> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <uses-permission android:name="android.permission.USB_PERMISSION" /> 
    <uses-feature android:name="android.hardware.usb.host" /> 
    <uses-feature android:name="android.hardware.usb.accessory" /> 
    <uses-feature android:name="android.hardware.usb.device" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/my_icon" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="mypackage.DumpActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleTask" 
      android:screenOrientation="landscape" 
      android:theme="@android:style/Theme.Black.NoTitleBar" > 
      <intent-filter> 
       <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> 
       <action android:name="android.hardware.usb.action.USB_DEVICE_DETTACHED" /> 
      </intent-filter> 
      <meta-data 
       android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
       android:resource="@xml/lib_device_filter"> 
      </meta-data> 
      <meta-data 
       android:name="android.hardware.usb.action.USB_DEVICE_DETTACHED" 
       android:resource="@xml/lib_device_filter"> 
      </meta-data> 
     </activity> 
     <activity 
      android:name="mypackage.ActivityA" 
      android:icon="@drawable/my_icon" 
      android:label="@string/ActivityA" 
      android:launchMode="singleTask" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="mypackage.ActivityB" 
      android:icon="@drawable/another_icon" 
      android:label="@string/ActivityB" 
      android:launchMode="singleTask" 
      android:screenOrientation="landscape" > 
     </activity> 
    </application> 
</manifest> 

참고 1 : 텐트 필터는이 활동에 있어야합니다!

참고 2 :이 솔루션은 다른 활동에서 USB와 상호 작용해야 할 때 유용하며 USB를 연결할 때마다 팝업 대화 상자를보고 싶지 않을 때 유용합니다.

마크 G.