使用Alarm Manager进行Android Set事件?

时间:2014-01-07 06:40:27

标签: android events broadcastreceiver alarmmanager reminders

我正在尝试制作一个提醒事件,该事件会在我的应用程序中的给定时间显示通知,为此示例我将Calendar实例设置为当前时间的一分钟。这是我的appointment.java代码,这是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中有以下代码作为BroadcastReceiver。

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"/>

我遇到的问题是没有显示任何通知或任何事情所以我不确定我是否做错了什么

此外,如果我在帖子中犯了错误,如果我在问题中的格式出错,请耐心等待。

修改

哇解决了感谢帮助人员,摆脱广播接收器,只是使用了服务虽然它仍然没有工作在最后我意识到我的机器人清单中有一个小错误

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

如果您发现我忘记指定我的服务包名称应该是myCF.MyAlarmService

感谢大家的帮助,我非常感谢

4 个答案:

答案 0 :(得分:0)

请遵循以下代码:

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

Sam的礼貌 https://stackoverflow.com/a/13593926/1465910

答案 1 :(得分:0)

您需要在服务中注册广播,类似于以下内容: register receiver in a service

答案 2 :(得分:0)

试试这个,将你的广播接收器类替换为服务“

(AlarmRec.class ===&gt; 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.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), 5000, pendingIntent);

答案 3 :(得分:0)

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

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:

    @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;

    }

它会起作用。试试看,让我知道。