广播接收器从不接收意图

时间:2016-05-27 16:29:31

标签: 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");

}

AlarmService

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>
  <receiver android:name=".AlarmReceiver"     
  android:enabled="true"/>
  <service android:name=".AlarmService" />
</application>

1 个答案:

答案 0 :(得分:2)

您正在使用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来阻止这种情况。

您不必担心重复警报,如果您使用相同的意图设置新警报,则使用相同意图的上一个警报将被取消。