Android USB大容量存储弹出

时间:2012-11-28 05:25:42

标签: android popup usb

当usb大容量存储设备连接到设备时,我正在尝试创建一个弹出窗口。我正在尝试 ACTION_MEDIA_MOUNTED 。我能够看到ACTION_MEDIA_MOUNTED来自日志,但我的应用程序没有弹出。 这是我的代码。
提前谢谢......

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

2 个答案:

答案 0 :(得分:1)

我刚从getExternalStorageState()而不是intent.getAction()中提取了intent,代码正常运行

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

        }

答案 1 :(得分:0)

您可以尝试从BroadcastReceiver

开始活动
Intent intent = new Intent(context,FileExplore.class);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);