George Halls
George Halls

Reputation: 11

Android Notification starting up on launch instead of at the set time

I am currently trying to create a repeating alarm that goes off at the same time everyday following a tutorial however I cannot seem to get it working. It goes off but as soon as the application launches instead of the set time I am not sure what the reason is. I also made sure the service was declared in manifest. Any idea where I am going wrong or if this the right way to go about something like this?

Main acitivty

public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    int i = preferences.getInt("numberofLaunces", 1);

    if (i < 2) {
        alarmMethod();
        i++;
        editor.putInt("numberoflaunches", i);
        editor.commit();
    }

    if (savedInstanceState == null) {
        return;
    }

}



private void alarmMethod() {
    Intent myIntent = new Intent(this, NotifyService.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 32);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.DAY_OF_MONTH, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

    Toast.makeText(MainActivity.this, "start Alarm", Toast.LENGTH_LONG).show();
}
}

Notifyservice

public class NotifyService extends Service {
@Override
public IBinder onBind(Intent Intent) {
    return null;
}

@Override
public void onCreate(){
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1,0);

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("placeholder tital")
            .setContentText("Placeholder text")
            .setSmallIcon(R.drawable.ic_cat)
            .setContentIntent(pIntent)
            .setSound(sound)
            .addAction(0,"hello", pIntent)
            .build();

    mNM.notify(1, mNotify);



}
}

Upvotes: 0

Views: 101

Answers (2)

Micha F.
Micha F.

Reputation: 634

The problem is quite simple: If the time a alarm should fire (the second parameter in setRepeating()) is in the past, it will fire immediately. The time you specified is 0:32 am in the night of the 0th (?) day of the month. This is always in the past (maybe except for 32 minutes every month, though I don't know what the 0th day of the month is).

To fire the alarm every 24 hours at the time of 0:32 am use something like this:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, new Date().after(calendar.getTime()) ? 1 : 0); //if the current time is after 0:32 am, one day is added

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

Upvotes: 0

ugur
ugur

Reputation: 3654

From developer [site](http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent))

The alarm is an Intent broadcast that goes to a broadcast receiver that you registered with registerReceiver(BroadcastReceiver, IntentFilter) or through the tag in an AndroidManifest.xml file.

You should define broadcast pending intent instead of service. Change this

Intent myIntent = new Intent(this, NotifyService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

To this

Intent myIntent = new Intent(this, MyAlarmBroadcastReceiver.class);
 pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);

Then implement MyAlarmBroadcastReceiver extends BroadcastReceiver class and fire notification inside onReceive().

Upvotes: 1

Related Questions