1

我试图通过设置警报在某个时间呼叫广播接收器。它似乎根本没有被调用。我正在尝试使用 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>

任何帮助表示赞赏。

4

1 回答 1

-1

考虑一下,我不确定您是否可以在执行此操作时BroadcastReceiver使用显式发送广播Intent...

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");
于 2012-11-10T17:46:52.750 回答