2

我有以下代码:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()))); 在我的清单中,我添加了:

           <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.ACTION_MEDIA_MOUNTED"/>
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <action android:name="android.intent.ACTION_MEDIA_SCANNER_SCAN_FILE"/>
            <data android:scheme="file"/>
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我收到以下错误

07-20 13:16:31.203: WARN/BroadcastQueue(395): Permission Denial: broadcasting Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///storage/sdcard0 flg=0x10 } from com.example.Music_Exchanger (pid=11797, uid=10080) is not exported from uid 10016 due to receiver com.android.providers.downloads/com.android.providers.downloads.DownloadReceiver

不知道为什么会被拒。我该如何解决?

如果你知道,请帮助我,并请写得更详细。我只是一个初学者。

非常感谢!

4

3 回答 3

2

实现广播接收器并将其定义添加到清单文件中:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <!--...-->

    <receiver
        android:name=".CustomReceiver"
        android:exported="true" >
    </receiver>
</application>

CustomReciever 扩展 BroadcastReceiver 的地方。

于 2013-07-22T18:39:31.813 回答
1

添加

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

另注:写入外部存储隐含已读取外部存储权限。

于 2013-07-20T05:52:13.383 回答
0

使用 MediaScannerConnection 而不是 SendBroadcast..

conn = new MediaScannerConnection( * * your context * * ,
     new MediaScannerConnection.MediaScannerConnectionClient() {@
         Override
         public void onScanCompleted(String path, Uri uri) {

             if (path.equals( * * your filename * * .getAbsolutePath())) {
                 Log.i("Scan Status", "Completed");
                 Log.i("uri: ", uri.toString());

                 conn.disconnect();
             }
         }@
         Override
         public void onMediaScannerConnected() {
             // TODO Auto-generated method stub
             conn.scanFile( * * your file name * * .getAbsolutePath(), null);

         }
     });
 conn.connect();
于 2014-08-13T08:59:54.790 回答