Android - BroadcastReceiver无法接收自定义意图

5
我有:
MyApp继承自Application并带有onCreate方法:
sendBroadcast(refreshAlarm);

Log.d(TAG, "broadcast sent with intent " + refreshAlarm);
Log.d(TAG, "onCreate");

在哪里

static final Intent refreshAlarm = new Intent(ACTION_REFRESH_RECEIVER);
public static final String ACTION_REFRESH_RECEIVER = "com.example.myapp.REFRESH_RECEIVER";

BroadcastReceiver :

package com.example.myapp;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;

public class RefreshReceiver extends BroadcastReceiver
{
    private static final String TAG = "RefreshReceiver";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, "broadcast received with intent " + intent);
        long interval = Long
                .parseLong(PreferenceManager.getDefaultSharedPreferences(
                        context).getString("delay", "900")) * 1000;

        PendingIntent operation = PendingIntent.getService(context, -1,
                new Intent(context, RefreshService.class),
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        alarmManager.cancel(operation);

        if (interval > 0)
        {
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis(), interval, operation);
        }

        Log.d(TAG, "onReceive: delay = " + interval);
    }
}

在清单中声明:

<receiver android:name="com.example.myapp.RefreshReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="com.example.myapp.REFRESH_RECEIVER" />
    </intent-filter>
</receiver>

我觉得我已经有了所有需要让这个工作的东西。在 onCreate 中发送了广播(我可以在日志中看到确实发送了)。广播被声明为接收 refreshAlarm 意图的意图过滤器,但它没有接收到,我无法弄清楚为什么。我还需要其他东西吗?

5个回答

4
如果您在mainfest.xml中放置了BroadCastReceiver,则不需要在代码中注册它。只有在您在代码中创建它时才需要进行注册。
以下是一个示例:
 <receiver android:name="MyReceiver" >
        <intent-filter>
            <action android:name="android.mybroadcast" />
        </intent-filter>
    </receiver>

在这里,您可以从您的类文件中调用它,

Intent intent = new Intent();
intent.setAction("android.mybroadcast");
sendBroadcast(intent);

1
它没起作用。我想这正是我用static final Intent refreshAlarm = new Intent(ACTION_REFRESH_RECEIVER);所做的事情。 - kostek
我尝试了这段代码,它可以正常工作。如果你无法正常运行,请提供包含sendBroadcast的活动代码和完整的mainfest.xml文件。 - mohammed momn
我在MyApp扩展Application类的onCreate方法中执行此操作(而不是Activity,因此可能是这种情况)。我正在尝试在创建应用程序时发送广播。 - kostek

1
尝试通过编程注册您的广播接收器。
public void registerBroadcastReceiver(View view) {

    this.registerReceiver(broadCastReceiver, new IntentFilter(
            "com.example.myapp.REFRESH_RECEIVER"));
    Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT)
            .show();
}

通过这个取消注册

        public void unregisterBroadcastReceiver(View view) {

    this.unregisterReceiver(broadCastReceiver);

    Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT)
            .show();
}

我应该在哪里做这个? - kostek
2
在你的代码中,从你的onCreate活动调用registerBroadcast方法,在你的onDestroy方法中调用unregisterBroadcast...不要在你的清单文件中注册它,而是在类内部注册它...请参考文档.. - Swap-IOS-Android
我怎么才能获取“broadCastReceiver”? - kostek
相同的结果。在日志中,我收到了消息“broadcast sent with intent Intent { act=com.example.myapp.REFRESH_RECEIVER }”,但是它没有被接收。顺便问一下,在清单中声明接收器有什么问题? - kostek
我在Application对象的onCreate中广播通知。也许这就是原因...? - kostek
显示剩余2条评论

1
可能是因为您的接收器和在清单文件中注册时使用的包名不匹配,导致它无法正常工作。
请确保您的包名与接收器所在的相应包名匹配。

0

我曾经遇到过同样的问题。似乎注册的广播接收器只对系统操作起作用。对于我的自定义操作,只有在我的应用程序运行时(活动在活动堆栈中)才能正常工作。

我的解决方法是为此创建一个活动。虽然这不能在后台工作,但它可以让您捕获来自其他应用程序的事件。

    <activity android:name=".ImportToProActivity">
        <intent-filter>
            <action android:name="com.sourcecastle.logbook.ImportToProActivity"/>

        </intent-filter>
    </activity>


 Intent intent = new Intent();
 intent.setComponent(new ComponentName("com.sourcecastle.freelogbook", "com.sourcecastle.logbook.ImportToProActivity"));
 startActivity(intent);

0

你说:

MyApp 继承 Application

你应该在 Android Manifest 中将 MyApp 注册为应用程序。

将此行放入 Android Manifest 应用程序元素中。

android:name="your.package.MyApp"

其中 your.package 是 MyApp 文件所在的包。现在,您的应用程序元素应如下所示

<application
    android:name="your.package.MyApp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/yourTheme" >

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