简体   繁体   中英

Notification not showing up at specific time using AlarmManager and BroadCast receiver

this is where i call broad cast receiver class

private void createAlarmNotification() {

    // TODO Auto-generated method stub
        Intent myIntent = new Intent(getApplication() , AlarmSet.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplication(), 0, myIntent, 0);
       Calendar calendar1 = Calendar.getInstance();

       Calendar calendar = Calendar.getInstance();

       calendar.set(2015, 3, 20, 23, 55, 00);
       Alarm a= new Alarm();
       a.SetAlarm(MainActivity.this, calendar.getTimeInMillis(), 60*1000);


}

this is my broadcast receviver class

public class Alarm extends BroadcastReceiver {

private final long GMT6 = AlarmManager.INTERVAL_HOUR * 6;
private final long DAY = AlarmManager.INTERVAL_DAY;

private ActivityManager activityManager;
private Context context;


@Override
public void onReceive(Context context, Intent intent) {

    this.context = context;
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    showNotification();

    wl.release();
}


public void SetAlarm(Context context, long userHour, long userMinute) {
    this.context = context;
    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setExact(AlarmManager.RTC_WAKEUP, userHour, pi);

}



public void showNotification() {

    Uri soundUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pIntent = PendingIntent
            .getActivity(context, 0, intent, 0);

    Notification mNotification = new Notification.Builder(context)
            .setContentTitle("")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
            .setSound(soundUri)
            .addAction(R.drawable.ic_launcher, "View", pIntent)
            .addAction(0, "Remind", pIntent).build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}


}

}

this is manifest code

<receiver android:name="com.example.notification.Alarm"
    android:enabled="true"
    android:exported="true" ></receiver>

but the broadcast receiver class dont show any kind of notification. It will be helpful to get proper suggestion for solving the problem.

In this call

calendar.set(2015, 3, 20, 23, 55, 00);

you are setting calendar's month to April, not March, that's why it is not triggered. If you want to set alarm to 60 seconds later, you may just write this:

    Calendar calendar = Calendar.getInstance();
    a.SetAlarm(MainActivity.this, calendar.getTimeInMillis(), 60*1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM