java - 广播接收者永远不会收到 Intent

标签 java android notifications

我在接收通知时遇到问题。我确信它已发送,因为我看到了发送日志。但我从未收到它,因为我从未看到日志或通知。我不确定自己做错了什么。 我不想重复闹钟。
在 Marshmallow 上使用 Nexus 7。

PracticeWordsActivity.java

public void setAlarm(int day){
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmIntent = new Intent(PracticeWordsActivity.this,AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

    //alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis()+15000, pendingIntent);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+15000, pendingIntent);
    Log.e(TAG,"Lift off");

}

报警服务

public class AlarmService extends IntentService 
{

private static final int NOTIFICATION_ID = 1;
private static final String TAG = "AlarmService";
private NotificationManager notificationManager;
private PendingIntent pendingIntent;


public AlarmService() {
      super("AlarmService");
  }


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
   return super.onStartCommand(intent,flags,startId);
}

@Override
protected void onHandleIntent(Intent intent) {
   Log.e(TAG,"Alarm Service has started.");
   Context context = this.getApplicationContext();
   notificationManager =         
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent mIntent = new Intent(this,MainActivity.class);
    Bundle bundle = new Bundle(); 
    bundle.putString("test", "test");
    mIntent.putExtras(bundle);
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);     

    Resources res = this.getResources();
    NotificationCompat.Builder builder = new     NotificationCompat.Builder(this);

    builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker("TITLE")
                .setAutoCancel(true)
                .setContentTitle("TITLE2")
                .setContentText("SUBJECT");

    notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());

Log.e(TAG,"Notifications sent.");
}

AlarmReceiver.java

 public class AlarmReceiver extends WakefulBroadcastReceiver {
   private static final String TAG = "AlarmReceiver";
   Intent intent;
   PendingIntent pendingIntent;
   NotificationManager notificationManager;

   @Override
   public void onReceive(Context context, Intent intent) {
     Log.e(TAG, "BroadcastReceiver has received alarm intent.");
     Intent service1 = new Intent(context, AlarmService.class);
     context.startService(service1);            
   }
}

AndroidManifest.xml

在 AndroidManifest 中声明最后一个 Activity 后,我已经设置了接收器和服务。

...
</activity>
  <receiver android:name=".AlarmReceiver"     
  android:enabled="true"/>
  <service android:name=".AlarmService" />
</application>

最佳答案

您正在使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 设置闹钟。 ELAPSED_REALTIME_WAKEUP 表示设备启动后耗时。 如果您希望闹钟在设置后 15 秒触发,请使用:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+15000, pendingIntent);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+15000, pendingIntent);

此外,我发现您正在 WAKEUP 模式下触发闹钟,并且您正在广播接收器中启动一项服务。设备有可能在服务启动之前重新进入休眠状态。您可以使用WakefulBroadcastReceiver为了防止这种情况发生。

您无需担心重复闹钟,如果您使用相同的 Intent 设置新闹钟,则之前使用相同 Intent 的闹钟将被取消。

关于java - 广播接收者永远不会收到 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488359/

相关文章:

windows - 使用 Ctrl+Alt+Del 获取通知

java - AsyncContext 响应与原始传入请求不匹配?

Android SearchSuggestSelection 启用查询以开头而不是包含搜索

android - Kotlin 多层 IT 引用

android - ?attr/selectableItemBackground 的定义和确切功能

java - 重新登录时处理通知数据 Android

java - H2:在所有其他计算机上均出现错误

java - 如果未找到 @Named,则使用 dagger 默认实现

java - 通过 MSGraph API 创建组也不会创建共享点

jquery - 某些 HTML 中的 CSS 通知设计问题