MediaScannerConnection.scanFile()はBroadcastRecieverから呼び出せない件

DownloadManagerで画像をダウンロードした後、BroadcastRecieverで完了通知を受け取ってすぐにコンテンツURI(content://)を得るために、MediaScannerConnection.scanFile()を呼んだらエラーになる。

01-26 13:28:00.159: E/AndroidRuntime(11034): FATAL EXCEPTION: main
01-26 13:28:00.159: E/AndroidRuntime(11034): java.lang.RuntimeException: Unable to start receiver com.example.DownloadManagerBroadcastReceiver: android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to bind to services

BroadcastRecieverからサービスを呼び出すことは許されていない、とな。実際、そのようだ。

BroadcastReceiver | Android Developers

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

その代わり、サービスに何かさせたいならIntentを使え!と。

そうだ。そうだったんだよ。これは前にも試してたんだ。

コメント

このブログの人気の投稿

レオナルド・ダ・ビンチはなぜノートを「鏡文字」で書いたのか?

macでsmb(samba)共有サーバーに別名で接続(別アカウント名で接続)する方法

Google DriveにCURLでアップロードするには?