Service in an AlarmReceiver is not getting called in Android

时间:2016-02-12 20:07:20

标签: android push-notification android-notifications android-alarms postdelayed

In my app, this is how notifications should work:

  • An AlarmReceiver gets triggered at a fixed time period in the day.
  • This AlarmReceiver start a service.
  • This service calls a post() method to delay the execution by 5 seconds.
  • The code in this post() method is responsible for firing a notification to the user.

Now, the issue is that the notifications are not seen on all phones. In fact, it is only being seen on a handful of phones. Furthermore, some phones show erratic behavior - sometimes showing it and other times not showing it.

Can someone please help me here?

EDIT: There is an AlarmManager which is setup and it calls this:

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    SimpleAlarmManager.INTERVAL_DAY,
    PendingIntent.getBroadcast(context, id,
        new Intent(context, AlarmReceiver.class),
    PendingIntent.FLAG_NO_CREATE);

AlarmReceiver.java:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context arg0, final Intent arg1) {
    Intent background = new Intent(arg0, CallNotificationAfter1sec.class);
    arg0.startService(background);
}
}

CallNotificationAfter1sec.java:

public class CallNotificationAfter1sec extends Service {
Context context;

@Override
public IBinder onBind(Intent intent) {
    return null;
}
@Override
public int onStartCommand( final Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)...
        }
    }, 1000);
    stopSelf();
    return START_STICKY;
}
@Override
public void onCreate() {
    this.context = this;
}
@Override
public void onDestroy() {
}
}

2 个答案:

答案 0 :(得分:0)

If the device is sleeping when the AlarmReceiver gets fired, it's only guaranteed to wake up for the onReceive() call of the AlarmReceiver, and it might go back to sleep instantly after that call completes.

Starting a service is an asynchronous call, so the phone might have gone back to sleep before starting the service.

Note the common use of the word "might", as the time the device is awake can be very different from time to time, and phone to phone.

To fix your issue, you need to hold a wakelock. Basically you manually ask the phone to stay awake untill you tell it to go to sleep again. But be very careful of this, and be sure to tell it to sleep again, or you'll be draining ALOT of battery.

I used the following code in one of my old projects to do exactly this:

Manifest:

checkBox.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            checkBox.setChecked(!checkBox.isChecked());
            ...
            return true;
        }
    });

Helper class:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

That should be able to fix all your problems.

Hope you get it to work.

答案 1 :(得分:0)

As documented, you need to set the following for a notification to work. Are you setting them all?

Required notification contents

A Notification object must contain the following:

  1. A small icon, set by setSmallIcon()
  2. A title, set by setContentTitle()
  3. Detail text, set by setContentText()