Search code examples
javaandroidbroadcastreceiveralarmmanager

AlarmManager doesn't seem to be working


I'm trying to schedule a method to be called using AlarmManager, but it doesn't seem to be working. I've looked at other examples and theirs isn't working for me. So I'm thinking it's something from my code. Here's the AlarmManager code:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

And my broadcast receiver:

public class KinectReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_notify)
                .setContentTitle("Kinect")
                .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setWhen(System.currentTimeMillis())
                .setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
                .setAutoCancel(true)
                .setSound(uri)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
                .setVibrate(new long[] { 100, 500, 100, 500, 100 });

        Intent targetIntent = new Intent(context, MainActivity.class);
        targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        targetIntent.putExtra("action", "main");
        targetIntent.putExtra("id", "");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

Neither the Toast nor the Notification show.

The AlarmManager code is in the first thing that runs in my launcher activity, in case that helps. Thanks


Solution

  • Please register ur receiver in manifeast file like this

    In application tag

    <receiver
       android:name=".KinectReceiver">