应用程序死亡后每天重复通知

时间:2018-04-13 09:52:50

标签: android notifications repeat

我每天都需要知道如何通知 我尝试使用AlarmManager,但是当应用程序关闭时,AlarmManager不起作用 我尝试使用服务,但也做同样的事情,也许我没有正确的服务。

我正在尝试使用小米Mi6,当打开保存时,服务可能会消失 我需要每天早上8点启动通知。

public class NotificationService extends Service {

final Handler handler = new Handler();


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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    startTimer(NotificationService.this);
    return START_STICKY;
}

public void startTimer(final NotificationService notificationService) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    if (calendar.get(Calendar.HOUR_OF_DAY) >= 8) {
        calendar.add(Calendar.DATE, 1);
    }

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


    long delay = calendar.getTimeInMillis() - System.currentTimeMillis();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            notification(notificationService);
            handler.postDelayed(this, 86400000); // 24 
        }
    };

    if(delay > 0) {
        handler.postDelayed(runnable, delay);
    }
}

public void notification(Context ctx) {
    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(ctx, Splash.class); pulsar en la notificación
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    String[] frasesPosibles = ctx.getResources().getStringArray(R.array.notificacionFrase);
    int rnd = new Random().nextInt(frasesPosibles.length);
    String frase = frasesPosibles[rnd];

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            ctx).setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(ctx.getResources().getString(R.string.newPhraseTitle))
            .setContentText(frase).setSound(alarmSound)
            .setAutoCancel(true).setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());
}

@Override
public void onCreate() {
}

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

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}

0 个答案:

没有答案