Frage

Good afternoon. I want to get a notification at 17:00 for instance. I'm using Notification and AlarmManager , but it doesn't work.

OnCreate in LoginActivity(it calls when app starts)

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, 12);
    calendar.set(Calendar.YEAR, 2013);
    calendar.set(Calendar.DAY_OF_MONTH, 3);

    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 13);

    calendar.set(Calendar.AM_PM, Calendar.PM);

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

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

MyReceiver :

public class MyReceiver extends BroadcastReceiver {

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

    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);

 }
}

MyAlarmService

public class MyAlarmService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) { return null; }

@Override
public void onCreate() { super.onCreate(); }

@Override
public void onDestroy() { super.onDestroy(); }


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    super.onStartCommand(intent, flags, startId);
    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), LoginActivity.class);
    Notification notification = new Notification.Builder(this.getApplicationContext()).
            setContentTitle("Texter").setContentText("OK").setSmallIcon(R.drawable.ic_launcher).build();
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mManager.notify(0, notification);

    return startId;


} }

In manifest

 <service android:name=".MyAlarmService"
        android:enabled="true"></service>
    <receiver  android:name=".MyReceiver"></receiver>

Thanks.

War es hilfreich?

Lösung

You should set :

calendar.set(Calendar.MONTH, 11); or calendar.set(Calendar.MONTH, Calender.DECEMBER);

the numbering for months start from 0

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top