2014-06-19 116 views
2

我已经在Boot上完成了android的9:00 AM闹钟。但是在启动完成后每分钟都会发出警报。每天在特定时间设置开机时间闹钟

我的要求是它应该设置开机后报警,但只在上午9:00发出报警。

这里是我的代码: public class AlarmUtil {0}私人PendingIntent alarmIntent;

public static void setAlarm(Context context) { 

    AlarmManager alarmManager = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 9); 
    calendar.set(Calendar.MINUTE, 00); 
    calendar.set(Calendar.SECOND, 00); 
    calendar.set(Calendar.AM_PM, Calendar.AM); 
    //calendar.setTimeInMillis(calendar.getTimeInMillis()); 
    calendar.add(Calendar.SECOND, 1); 

    Intent intent = new Intent(context, Services.class); 
    PendingIntent pintent = PendingIntent.getService(context, 123456789, 
      intent, 0); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), 1 * 60 * 1000, pintent); 

} 
} 

public class AlarmBroadcastReciever extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
     Toast.makeText(context, "Booted!", Toast.LENGTH_SHORT).show(); 
     AlarmUtil.setAlarm(context); 

    } 
} 
} 

服务(我的服务类)

public class Services extends IntentService { 

public Services(String name) { 
    super("services"); 
    // TODO Auto-generated constructor stub 
} 

public Services() { 
    super("services"); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    MyApp.counter += 1; 

    Toast.makeText(getApplicationContext(), 
      "Service started: " + MyApp.counter, Toast.LENGTH_LONG).show(); 
    return START_STICKY; 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Toast.makeText(getApplicationContext(), "handling intent", 
      Toast.LENGTH_LONG).show(); 

} 

}

我欠缺的我。请帮帮我。提前致谢。

+0

面貌产生报警在这个文件http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29 –

回答

1

您好我已经使用下面的代码,并能够在每天上午9点

alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(this, AlarmService.class); 

    PendingIntent pintent = PendingIntent.getService(this, 123456789, 
      intent, 0); 

    // Set the alarm to start at 8:30 a.m. 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 9); 
    calendar.set(Calendar.MINUTE, 00); 


    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), 1000 * 60 * 1 * 60 * 24, pintent); 
5
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
     calendar.getTimeInMillis(), 1 * 60 * 1000, pintent); 

通过调用setRepeating(),您指示它每分钟启动一次意图。您可能打算使用set()setExact()代替。

此外,您正在为当前日期上的9:00 设置闹钟。我不完全确定你的目标是什么,但是如果你想为“下一个9点”发出警报(即作为闹钟),如果当前时间大于9点,你应该增加一天。从文档:

如果规定的触发时间过去,将立即触发报警 。

编辑:如果你需要的是火灾报警每天9:00,然后setRepeating()是正确的,但以毫秒为单位的时间应该AlarmManager.INTERVAL_DAY。尽管如此,请注意关于过去警报的提示。如果您启动手机,例如在上午10:00,并且您使用当前的代码,则除了未来的代码之外,您还会收到一个警报。

而且,正如@DerGolem指出的那样,如果您定位API级别19,那么这些警报将是不精确的(并且不存在setRepeatingExact())。如果你需要确切的报警,那么你将不得不安排一个与setExact(),然后下一个当这一个火灾,等等。

+0

我刚刚添加我最小差距只是用于测试目的。问题是如果我在8:56 AM重新启动系统。 setAlarm()将会被调用,然后它将在每分钟后启动服务。可以说开机时间是早上8点56分,我想在上午9点闹钟。它将在8点57分然后8点58分然后8点59分发出警报。 –

+0

@DeepikaLalra我不知道我的理解。如果你想要一个报警,使用'set()'。 'setRepeating()'会触发重复的警报。这不是你目前遇到的问题吗? – matiash

+0

'注意:从API 19(KITKAT)开始,报警传送是不准确的:操作系统将会移动报警以最大限度地减少唤醒和电池使用。有新的API支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 targetSdkVersion早于API 19的应用程序将继续看到之前的行为,即所有报警都是在请求时准确传送的。' –

1

需要更换“1 * 60 * 1000”与AlarmManager.INTERVAL_DAY每天设置

相关问题