广播接收器没有被警报调用

时间:2012-11-10 16:49:23

标签: android

我试图通过设置闹钟在特定时间呼叫广播接收器。它似乎根本没有被调用。我正在尝试使用logcat进行调试。

我对警报的设置是这样的:

    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar notifyAlarm = Calendar.getInstance();

    notifyAlarm.set(Calendar.HOUR, 17);
    notifyAlarm.set(Calendar.MINUTE, 00);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 2378530, intent, 0);
    alarm.set(AlarmManager.RTC_WAKEUP, notifyAlarm.getTimeInMillis(), pendingIntent);

我的接收者如下:

    public class AlarmReceiver extends BroadcastReceiver {

private static final String TAG ="test receiver";
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    Log.v(TAG, "receiver called");

}



    }

也在我的清单中:

    </activity>
    <receiver android:name="AlarmReceiver"></receiver>
</application>

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

考虑到这一点,我不确定您是否可以使用明确的BroadcastReceiverIntent发送广播...

Intent intent = new Intent(this, AlarmReceiver.class);

尝试向<receiver部分提供<intent-filter>示例...

<receiver android:name=".AlarmReceiver">
    <intent-filter>
        <action android:name="com.mypackage.ACTION_DO_SOMETHING" />
    </intent-filter>
</receiver>

然后,当您创建Intent时,请执行以下操作...

Intent intent = new Intent("com.mypackage.ACTION_DO_SOMETHING");