安卓USB大容量存储弹出窗口

4
我正在尝试在设备连接到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

你第一次运行Activity了吗?那时,Android系统将会首次为intent-filter注册BroadcastReceiver。 - RvdK
2个回答

1

我刚刚从getExternalStorageState()中提取了意图,而不是intent.getAction(),代码可以正常工作

 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

您可以尝试从BroadcastReceiver启动一个活动

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

我已经尝试过了,但结果还是一样......我需要在这里使用PendingIntent吗? - Pawan
@Pawan 这里不需要 PendingIntent,你可以发布 AndroidManifest.xml 来获取更多帮助。 - ρяσѕρєя K
@Pawan:你可以在AndroidManifest.xml中尝试使用<receiver android:name="com.mburman.fileexplore.MemStickReciever"><intent-filter android:priority="1000"><action android:name="android.intent.action.MEDIA_MOUNTED"/><data android:scheme="file"/></intent-filter></receiver> - ρяσѕρєя K
@Imram 谢谢,伙计,它正在工作。只是改变了 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)。 - Pawan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接