Java BroadcastReceiver工作不正常,而在其他情况下工作正常
我正在制作一个应用程序,当特定日期临近时,它需要通知用户 我使用Java BroadcastReceiver工作不正常,而在其他情况下工作正常,java,android,broadcastreceiver,alarmmanager,Java,Android,Broadcastreceiver,Alarmmanager,我正在制作一个应用程序,当特定日期临近时,它需要通知用户 我使用客户类作为活动和服务之间的“中间人”。绑定服务后,我调用一个与AlarmTask类交互的方法,该类使用AlarmManager设置报警。作为最后一步,我发送了一个挂起的内容来启动我的另一个类,它是我通知的广播接收器 我的问题是没有调用onReceive()。代码一直正确到达alarmManager.set()。我读了很多帖子,尝试了不同的方法来注册我的广播接收器。有什么想法可能是错误的吗 报警任务 public class Alar
客户
类作为活动和服务
之间的“中间人”。绑定服务后,我调用一个与AlarmTask类交互的方法,该类使用AlarmManager
设置报警。作为最后一步,我发送了一个挂起的内容来启动我的另一个类,它是我通知的广播接收器
我的问题是没有调用onReceive()
。代码一直正确到达alarmManager.set()
。我读了很多帖子,尝试了不同的方法来注册我的广播接收器。有什么想法可能是错误的吗
报警任务
public class AlarmTask implements Runnable {
private final Calendar date;
private final AlarmManager alarmManager;
private final Context context;
private long mGoalId;
public AlarmTask(Context context, Calendar date, long goalId) {
this.context = context;
this.alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
mGoalId = goalId;
}
@Override
public void run() {
Log.e("AlarmTask", "run executed with request code: " + mGoalId);
// Request to start the service when the alarm date is upon us
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.INTENT_NOTIFY, true);
// mGoalId is the unique goal id that is gonna be used for deletion
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int)mGoalId, intent, 0);
alarmManager.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}
}
通知接收者
public class NotificationReceiver extends BroadcastReceiver {
// Unique id to identify the notification.
private static final int NOTIFICATION = 123;
public static final String INTENT_NOTIFY = "com.test.name.services.INTENT_NOTIFY";
private NotificationManager notificationManager;
@Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive", "Broadcast fired : " + intent);
CharSequence title = "Alarm!!";
int icon = R.drawable.goal;
CharSequence text = "Your notification time is upon us.";
long time = System.currentTimeMillis();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(icon);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setVibrate(new long[] { 0, 200, 100, 200 });
final Notification notification = builder.build();
notificationManager.notify(NOTIFICATION, notification);
}
}
清单
<receiver android:name=".broadcasts.NotificationReceiver"></receiver>
我认为您需要在此接收器上设置一个过滤器,说明它需要对哪个意图做出反应<代码>您只需将操作设置为更适合您的操作。我尝试过使用过滤器(尽管我不确定什么更适合我)。我也试过这个
。我仍然无法在onReceived()
上获取日志。我认为您需要在该接收器上设置一个过滤器,说明它需要对哪个意图做出反应。例如<代码>
您只需将操作设置为更适合您的操作。我尝试过使用过滤器(尽管我不确定什么更适合我)。我也试过这个
。在onReceived()