如何在android上每天每小时给予背景通知(例如上午7点到上午8点,上午9点,上午9点00分)

如何在android上每天每小时给予背景通知(例如上午7点到上午8点,上午9点,上午9点00分)

问题描述:

我正在为我的大学做一个时间表项目,在这个项目中我想每个小时都会生成通知小时使用AlarmManager,我几乎找到了解决方案,但闹钟在随机时间内被触发,就像在几个小时之间一样。我想要的是每天准确地触发闹钟管理器消息,例如每天早上7点,8点,等等。我的代码如下。如何在android上每天每小时给予背景通知(例如上午7点到上午8点,上午9点,上午9点00分)

MainActivity.java

public class MainActivity extends AppCompatActivity { 

DatabaseHelper2 db = new DatabaseHelper2(this); 

private PendingIntent pendingIntent; 
private AlarmManager alarmManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    Cursor resService = db.getAllData(); 
    int pref = 0; 
    if(resService.getCount()!=0){ 

     Date date2 = new Date(); 
     int h=date2.getHours(); 
     int m = date2.getMinutes(); 
     int s = date2.getSeconds(); 

     Intent alarmIntent = new Intent(this,AlarmReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(this,0,alarmIntent,0); 
     alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     //int interval = 60 * 60 * 10000; 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent); 
     Toast.makeText(this,"Alarm set at "+h+" : "+m+" : "+s,Toast.LENGTH_LONG).show(); 
    } 


} 

AlarmReciever.java

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Date d = new Date(); 
     Toast.makeText(context,"Broadcast message triggered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds() ,Toast.LENGTH_SHORT).show(); 
     Intent service1 = new Intent(context, MyService.class); 
     context.startService(service1); 
    } 
} 

MyService.java

public class MyService extends Service { 
    DatabaseHelper2 db = new DatabaseHelper2(this); 
    String sub; 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Date date = new Date(); 
     int now=date.getHours(); 
     SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); 
     String dayOfWeek = sdf.format(date); 
     Cursor resNow = db.getNowSub(now,dayOfWeek); 

     if(resNow.getCount()!=0){ 
      if(resNow.moveToNext()){ 
       sub = resNow.getString(3); 
      } 
      NotificationManager notif = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notify = new Notification.Builder 
        (getApplicationContext()).setContentTitle("NowSub").setContentText("Now:"+sub+ " at "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()).setContentTitle("currenet subject").setSmallIcon(R.mipmap.ic_launcher).build(); 
      notify.flags |= Notification.FLAG_AUTO_CANCEL; 
      notif.notify(0, notify); 

     } 



     return START_STICKY; 

    } 

我为这个特殊问题花了一个多星期,希望有人能帮忙。 在此先感谢。

取而代之的是:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent); 

你需要传递的实际时间,当你想这个醒来,不System.currentTimeMillis()

所以像

Calendar calendar = Calendar.getInstance(); 
Date newDate = calendar.setTime(date2); 
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,calendar.getTimeInMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent); 

改为尝试此操作。我更改为AlarmManager.ELAPSED_REALTIME

+0

我试过了,但这并不是每个小时都执行完毕。 – Arshad

+0

@Arshad试试我的编辑 – bc004346

+1

对不起,迟到的代码为我工作,谢谢! – Arshad

如AlarmManager的描述:

注:与API开始19(KITKAT)报警输送是不精确的:操作系统 将以便最小化唤醒和电池使用移位警报。有 是新的API来支持需要严格交付 保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。其中 targetSdkVersion早于API 19的应用程序将继续看到 之前的行为,在该行为中, 请求时所有报警均准确传送。

我建议使用setExact而不是setRepeating在准确的时间被唤醒。

此外,检查出JobScheduler如下陈述:Android AlarmManager setExact() is not exact

+0

感谢您的回应,但我想每天重复闹钟,我认为setExact方法只会触发一次。 – Arshad

+0

可能JobScheduler可以重复触发器。 – JFPicard