Android重复闹钟或服务?

时间:2011-12-22 09:06:36

标签: android service notifications alarm

我创建了一个应用程序,它根据地理坐标计算一天中的特定时间。它会在这些时间发出通知。 我已经使用重复闹钟每日通知(虽然时间每天都在变化,因此不准确)。 但除非重置,否则3天后我没有通知我(我没有观察确切的天数)。为什么会这样? 目前我已决定使用每日设置通知的服务,因此准确无误。我如何编写服务并调用它?

public void alarmNoon(long diff) {
      Intent intent = new Intent(this, PNoon.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
      if (checkadan(1)==true){
          if (diff>=0){
          am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+diff,
            (1000*60*60*24), pendingIntent);}else{
          am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+diff+(1000*60*60*24), (1000*60*60*24), pendingIntent);
            }}else{
                am.cancel(pendingIntent);
            }
     }

diff是时间差异btwn中午和当前时间

public class PNoon extends BroadcastReceiver {
Dialog db;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
    Notification notification = new Notification( R.drawable.icon, "Time", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
    notification.setLatestEventInfo(context,  "Time", "Its Noon", contentIntent);      
    notification.flags=Notification.FLAG_AUTO_CANCEL;
    notification.sound=Uri.parse("android.resource://net.ttct.time/"+R.raw.NoonAnnouncement);
    mNotificationManager.notify(1, notification);

}
}

一旦拖动通知栏,声音就会停止,只有在通知栏中触摸了该项目时才会停止。

<receiver  android:process=":remote" android:name="PNoon"></receiver>

1 个答案:

答案 0 :(得分:0)

       PendingIntent.FLAG_CANCEL_CURRENT change it as intent.FLAG_NEW_TASK

并在manifest.xml中注册您的广播