2012-11-28 28 views
4

我想创建一个弹出窗口,当一个USB海量存储设备连接到设备。我正在尝试使用ACTION_MEDIA_MOUNTED。我能够看到ACTION_MEDIA_MOUNTED正在通过日志,但我的应用程序不会弹出。 这是我的代码。
在此先感谢...Android USB海量存储弹出

MemStickReciever.class

public class MemStickReciever extends BroadcastReceiver { 
public final String TAG = "usbfile"; 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(intent.ACTION_MEDIA_MOUNTED)) 
     { 

     Log.d(TAG,"USB MOUNTED (1)"); 
     Intent intentLaunch = new Intent(context,FileExplore.class); 
     Log.d(TAG,"intent has been created "); 
     context.startActivity(intentLaunch); 
     Log.d(TAG,"intent launched "); 
    } 

} 

FileExplore.class

public class FileExplore extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.mburman.fileexplore" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-feature android:name="android.hardware.usb.host" /> 
    <uses-sdk android:minSdkVersion="12" /> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".FileExplore" 
        android:label="@string/app_name"> 
       <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      </activity> 
     <receiver android:enabled="true" android:exported="true" android:name="MemStickReciever"> 
     <intent-filter> 
      <action android:name="android.intent.action.MEDIA_MOUNTED"/> 
      </intent-filter> 
    </receiver> 
    </application> 
</manifest> 

日志文件

D/dalvikvm(1552): GC_CONCURRENT freed 674K, 13% free 9676K/11015K, paused 1ms+3ms 
D/Vold (1504): Volume usb state changing 0 (No-Media) -> 2 (Pending) 
D/Vold (1504): Volume usb state changing 2 (Pending) -> 1 (Idle-Unmounted) 
D/VoldCmdListener(1504): volume mount /mnt/USB 
I/Vold (1504): /dev/block/vold/8:17 being considered for volume usb 
D/Vold (1504): Volume usb state changing 1 (Idle-Unmounted) -> 3 (Checking) 
D/MountService(1552): volume state changed for /mnt/USB (bad_removal -> checking) 
I//system/bin/fsck_msdos(1504): ** /dev/block/vold/8:17 
D/MountService(1552): sendStorageIntent Intent { act=android.intent.action.MEDIA_CHECKING dat=file:///mnt/USB (has extras) } 
I//system/bin/fsck_msdos(1504): ** Phase 1 - Read and Compare FATs 
I//system/bin/fsck_msdos(1504): Attempting to allocate 120 KB for FAT 
I//system/bin/fsck_msdos(1504): Attempting to allocate 120 KB for FAT 
I//system/bin/fsck_msdos(1504): ** Phase 2 - Check Cluster Chains 
I//system/bin/fsck_msdos(1504): ** Phase 3 - Checking Directories 
I//system/bin/fsck_msdos(1504): ** Phase 4 - Checking for Lost Files 
I//system/bin/fsck_msdos(1504): 22 files, 207512 free (25939 clusters) 
I/Vold (1504): Filesystem check completed OK 
I/Vold (1504): Device /dev/block/vold/8:17, target /mnt/USB mounted @ /mnt/secure/staging 
D/Vold (1504): Volume usb state changing 3 (Checking) -> 4 (Mounted) 
D/MountService(1552): volume state changed for /mnt/USB (checking -> mounted) 
D/MountService(1552): sendStorageIntent Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///mnt/USB (has extras) } 
D/MediaScannerReceiver(1969): action: android.intent.action.MEDIA_MOUNTED path: /mnt/USB 
I/MediaUploader(2289): No need to wake up 
D/dalvikvm(1969): GC_CONCURRENT freed 500K, 9% free 6646K/7239K, paused 0ms+1ms 
D/MediaStoreImportService(2419): Handle action: MediaStoreImportService.import_pending 
D/MediaStoreImportService(2419): onDestroy 
E/dalvikvm(1969): No JIT support for bytecode f0 at offsetPC 0 
E/dalvikvm(1969): JIT implementation not found 
I/dalvikvm(1969): codeGenBasicBlockJit returns negative number 
D/dalvikvm(1969): GC_CONCURRENT freed 317K, 8% free 6712K/7239K, paused 1ms+1ms 
D/MediaStoreImportService(2419): Scanner finished. Starting media store import 
D/MediaStoreImportService(2419): Handle action: MediaStoreImportService.import_pending 
D/MediaStoreImportService(2419): Handle action: MediaStoreImportService.import 
I/MediaStoreImporter(2419): Update: incremental Added music: 0 Updated music: 0 Deleted music: 0 Created playlists: 0 Updated playlists: 0 Deleted playlists: 0 Inserted playlist items: 0 Deleted playlist items: 0 Removed orphaned playlist items: 0 
D/MediaStoreImportService(2419): onDestroy 
+0

你有没有第一次运行你的Activity。那时,BroadcastReceiver将首次由Android系统注册到intent-filter。 – RvdK

回答

1

我刚刚从提取getExternalStorageState(意图开始活动)作品

public class MemStickReciever extends BroadcastReceiver { 
     public final String TAG = "usbfile"; 
     @Override 
     public void onReceive(Context context, Intent intent) { 

    //As through the logs when the usb mass storage is connected **sendStorageIntent Intent { act=android.intent.action.MEDIA_MOUNTED** was displayed in the log 

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 

       onMemcardMounted(); 
       Log.d(TAG,"USB MOUNTED (1)"); 
       Intent intentLaunch = new Intent(context,FileExplore.class); 
       intentLaunch.addFlags(Intent.FLAG_FROM_BACKGROUND); 
       intentLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       Log.d(TAG,"intent has been created "); 
       context.startActivity(intentLaunch); 
       Log.d(TAG,"intent launched "); 
      } 

     } 
0

你可以尝试,而不是intent.getAction()和代码从BroadcastReceiver

Intent intent = new Intent(context,FileExplore.class); 
intent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent); 
+0

我高压尝试过,但仍然是相同的结果...........我需要在这里使用PendingIntent – Pawan

+0

@Pawan没有PendingIntent这里不需要,你可以发布AndroidManifest.xml的更多帮助 –

+0

我hv附加这两个日志和manifest.xml文件 – Pawan