English 中文(简体)
不受地方广播的安乐团活动
原标题:Android activity not getting broadcast from local service

从这些例子来看,这是直截了当的。 也许你可以向我表明我错了。 我可以不参加从当地机构广播的活动。

我的活动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_FINISHEDcom.example.intent.action. DOWNLOAD_FINISHED"

服务1在接收器收到下载主管活动,显然播放了自己的习俗活动,但活动2似乎从未收到。 我有什么错误? 是否在加工过程中播放意图是个问题? (我不认为如此——那是不合时宜的,正确吗?)

Update: 为了保证在接受广播的中间不会出现发送广播的问题,我修改了我的广播法,以便在主线上进行三秒广播:

    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

但这项活动没有收到任何资料。 请提供帮助。

最佳回答

Eureka! 我发现这句话! 问题是,我提供了我广播意向中的一份数据。 甲型混凝土配对规则几乎变得复杂。 如果你提供URI数据,那么你的意图过滤器必须指明一个相应的MMIS类型。

遗憾的是,尽管安乐文文件说,数据类型可以从数据URI中推断出来,但显然安乐团知道,file http://.../example.jpg 是一种形象。 因此,这项工作没有:

intentFilter.addDataType("image/*");

然而,我不能具体指明一种类型,而是可以确定一个我接受的计划:

intentFilter.addDataScheme("file");

工作! 将我的广播限制在<代码>上: URIs,但随着时间的推移,它一直在运行。

Note that apparently I could manually specify the MIME type in the intent when I broadcast it, but that s too much trouble for now, as I m downloading images from Picasa so I already know that they are images (and don t care the specific MIME type). And if it gets too much trouble, I could ditch the whole setData() thing altogether and set an extra---but of course I want to do things the Right Way.

问题回答

你是否把你的接受者列入你的活动?

<receiver
  android:name=".YourReceiver">
  <intent-filter>
    <action
      android:name="intent_name"></action>
  </intent-filter>
</receiver>




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签