5

我正在使用此通知代码:

package com.example.mega;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
         nm = (NotificationManager) context
             .getSystemService(Context.NOTIFICATION_SERVICE);
         CharSequence from = "Tech";
         CharSequence message = "Check out our NEW COLLECTION !!";
         PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
             new Intent(), 0);
         Notification notif = new Notification(R.drawable.icon,
             "Check out our NEW COLLECTION !!" , System.currentTimeMillis());
         notif.setLatestEventInfo(context, from, message, contentIntent);
         nm.notify(1, notif);
    }
}

如您所见,我还没有添加 AlarmManager,因为我没有使用它的经验,我是 android 新手。我应该在代码中添加什么以每 24 小时显示一次通知?

4

1 回答 1

4
Calendar calendar = Calendar.getInstance();
// 8.00 (8 AM) 
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

PendingIntent pi = PendingIntent.getService(context, 0 , new Intent(context, Your_Class.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                            AlarmManager.INTERVAL_DAY, pi);

这会在每天早上 8 点设置闹钟

或者您可以使用https://github.com/commonsguy/cwac-wakeful。查看链接上的文档。

看看这个:每 24 小时和早上 8 点的确切时间发出火灾通知

这:http ://blog.blundellapps.com/notification-for-a-user-chosen-time/

于 2013-08-03T15:48:58.623 回答