BroadcastReceiver未被Alarm(Manager)调用

时间:2017-01-03 19:18:55

标签: android broadcastreceiver alarmmanager android-pendingintent android-broadcastreceiver

当闹钟响起时,您能否发现为什么没有调用BroadcastReceiver的原因?如果我有一个明确的意图启动警报,它工作正常,我的活动打开。如果我设置打开我的BroadCastReceiver的意图,那么没有任何事情发生,所以我认为我的接收器类或Manifest可能有问题。以下是我设置闹钟的方法:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 324, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这是我的广播接收器:

public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("MJH", "Alarm called...");
        Toast.makeText(context, "Alarm...", Toast.LENGTH_LONG).show();
    }
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="mjh.com.apod"
      xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Required to act as a custom watch face. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <receiver
        android:name=".AlarmBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        android:process=":remote">
    </receiver>
</application>

非常感谢你的时间。

2 个答案:

答案 0 :(得分:2)

我认为您应该使用PendingIntent.getBroadcast(代替PendingIntent.getActivity(

答案 1 :(得分:0)

我不得不调用getBroadcast而不是getActivity来创建PendingIntent。修好了。