Android使用报警管理器设置事件?

Android使用报警管理器设置事件?,android,events,broadcastreceiver,alarmmanager,reminders,Android,Events,Broadcastreceiver,Alarmmanager,Reminders,我正在尝试创建一个提醒事件,该事件将在应用程序中的给定时间显示通知。在本例中,我将日历实例设置为当前时间的一分钟。这是我的appointment.javacode,这是Calendar的实例被初始化为当前时间+一分钟的地方 Calendar ctest = Calendar.getInstance(); ctest.add(Calendar.MINUTE, 1); Intent myIntent = new Intent(Appointments.this, AlarmRec.class); p

我正在尝试创建一个提醒事件,该事件将在应用程序中的给定时间显示通知。在本例中,我将
日历
实例设置为当前时间的一分钟。这是我的
appointment.java
code,这是
Calendar
的实例被初始化为当前时间+一分钟的地方

Calendar ctest = Calendar.getInstance();
ctest.add(Calendar.MINUTE, 1);
Intent myIntent = new Intent(Appointments.this, AlarmRec.class);
pendingIntent = PendingIntent.getBroadcast(Appointments.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, ctest.getTimeInMillis(), pendingIntent);
然后,我的
AlarmRec.class
中有以下代码作为广播接收器

public class AlarmRec extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {  
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
    }
}
最后,在我的
MyAlarmService.class
中,我有以下内容

public void onStart(Intent intent, int startId)
{
   super.onStart(intent, startId);

   mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
   Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
   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;
   notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

   mManager.notify(0, notification);
}
我的
AndroidManifest
包含

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

    <receiver android:name=".AlarmRec"/>

我遇到的问题是没有显示任何内容,没有通知或任何内容,因此我不确定我是否做错了什么

此外,如果我在我的帖子中犯了错误,请容忍我在问题的格式上犯了错误

编辑

哇哦,谢谢大家的帮助,摆脱了广播接收器,只是使用了这项服务,尽管它最终还是不起作用。我意识到我的android清单中有一个小的输入错误

如果您看到我忘记为服务指定包名,则应为myCF.MyAlarmService


谢谢大家的帮助我真的很感激

请遵循以下代码:

long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis * DateUtils.MINUTE_IN_MILLIS;
Maybe you meant for the alarm to go off in one minute:

long nextUpdateTimeMillis = currentTimeMillis + DateUtils.MINUTE_IN_MILLIS;
Anyway first use:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          System.currentTimeMillis() + 10000, 
                          5000,
                          pendingIntent);
To confirm your setup is correct, if so you need to recalculate your nextUpdateTimeMillis
    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 

    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   


        NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
           Intent intent1 = new Intent(this.getApplicationContext(),MainActivity1.class);

           Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
           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;
           notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

           mManager.notify(0, notification);
        return 0;

    }
由山姆提供

您需要在服务中注册广播,与此类似:

尝试此操作,更换您的广播接收器类以提供服务`

(AlarmRec.class==>MyAlarmService.class))`

编辑


从活动调用服务,因为不需要接收者:

Calendar ctest = Calendar.getInstance();
    ctest.add(Calendar.MINUTE, 1);
     Intent myIntent = new Intent(Appointments.this, MyAlarmService.class);
      pendingIntent = PendingIntent.getBroadcast(Appointments.this, 0, myIntent,0);
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, ctest.getTimeInMillis(), pendingIntent);
startService(myIntent );
之后,通过以下代码更改MyAlarmService.class:

long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis * DateUtils.MINUTE_IN_MILLIS;
Maybe you meant for the alarm to go off in one minute:

long nextUpdateTimeMillis = currentTimeMillis + DateUtils.MINUTE_IN_MILLIS;
Anyway first use:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          System.currentTimeMillis() + 10000, 
                          5000,
                          pendingIntent);
To confirm your setup is correct, if so you need to recalculate your nextUpdateTimeMillis
    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 

    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   


        NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
           Intent intent1 = new Intent(this.getApplicationContext(),MainActivity1.class);

           Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
           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;
           notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

           mManager.notify(0, notification);
        return 0;

    }

它会起作用的。试试这个,让我知道。

你有什么问题吗?没有显示任何通知没有发生任何事情为什么你在这里使用广播接收器你想在启动时启动服务?嗯,我想在预约设定后的任何时间启动服务如果我设定的时间太长,从现在开始10分钟,它应该会给我通知,如果我从现在开始3天,并且在这段时间内如果我重新启动手机,它仍会通知我广播接收器不是这种情况下的最佳选项吗?那么您不需要广播接收器管理器alarmManager=(alarmManager)getSystemService(ALARM_服务);alarmManager.setRepeating(alarmManager.RTC_唤醒,系统.currentTimeMillis()+10000,5000,PendingEvent);我试着这样做是为了不明白你的意思,我做了更改,但仍然没有出现:(在我给你代码之前,我已经在设备上测试了它。所以,对不起,我不知道更多。u和Satyaki Mukherjee都有相同的答案,但都不起作用:(我延迟了5秒(5000)它应该每5个扇区发射一次,它仍然不起作用:(也许是我的模拟器让我把它安装在我的设备上