从这些例子来看,这是直截了当的。 也许你可以向我表明我错了。 我可以不参加从当地机构广播的活动。
我的活动1
startService(new Intent(Activity1.this, Service1.class));
活动1随后开始 活动2:
startActivity(new Intent(Activity1.this, Activity2.class));
服务1是一个地方机构,负责倾听下载:
protected final BroadcastReceiver service2DownloadBroadcastReceiver = new BroadcastReceiver()
{
public void onReceive(final Context context, final Intent intent)
{
...
broadcastDownloadFinished(Uri.fromFile(downloadedFile));
广播接收器1 然后播放自己的信息:
protected Intent broadcastDownloadFinished(final Uri uri)
{
final Intent intent = new Intent(ACTION_DOWNLOAD_FINISHED).setData(checkNotNull(uri));
sendBroadcast(intent);
活动2当时处于地下,聆听了行动...... DOWNLOAD -FINISHED 意向,利用自己的广播接收器:
private final BroadcastReceiver activity2DownloadBroadcastReceiver = new BroadcastReceiver()
{
public void onReceive(final Context context, final Intent intent)
{
Log.i(Activity2.class.getSimpleName(), "Received download event: " + intent.getAction() + " " + intent.getData());
活动2当然登记接收人:
protected void onResume()
{
super.onResume();
final IntentFilter downloadIntentFilter = new IntentFilter();
downloadIntentFilter.addAction(ACTION_DOWNLOAD_FINISHED);
registerReceiver(activity2DownloadBroadcastReceiver, downloadIntentFilter);
如果有的话,ACTION_DOWNLOAD_FINISHED
是com.example.intent.action. DOWNLOAD_FINISHED"
。
服务1在接收器收到下载主管活动,显然播放了自己的习俗活动,但活动2似乎从未收到。 我有什么错误? 是否在加工过程中播放意图是个问题? (我不认为如此——那是不合时宜的,正确吗?)
Log.i(getClass().getSimpleName(), "...ready to broadcast");
final Intent intent = new Intent(ACTION_DOWNLOAD_FINISHED).setData(checkNotNull(uri));
mainThreadHandler.postDelayed(new Runnable()
{
public void run()
{
Log.i(getClass().getSimpleName(), "...broadcasting");
sendBroadcast(intent);
Log.i(getClass().getSimpleName(), "...broadcasted");
}
}, 3000);
Log.i(getClass().getSimpleName(), "...scheduled to broadcast");
如预期,该日志说:
...ready to broadcast
...scheduled to broadcast
...broadcasting
...broadcasted
但这项活动没有收到任何资料。 请提供帮助。