0

我正在创建一个应用程序,我必须每五分钟显示一次提醒。我必须每五分钟显示一次经度和纬度,而不是提醒。我有两个活动:一个是AlarmManager,另一个是AlarmManagerBroadcastReceiver。我该怎么做呢?

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver 
{

  String gps;

  final public static String ONE_TIME = "onetime";
  @Override
  public void onReceive(Context context, Intent intent)
  {

     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
     wl.acquire();

     Bundle extras = intent.getExtras();
     StringBuilder msgStr = new StringBuilder();

     if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
         msgStr.append("Reminder");
     }
     Format formatter = new SimpleDateFormat("hh:mm:ss a");
     msgStr.append(formatter.format(new Date()));

     Toast.makeText(context, "Reminder", Toast.LENGTH_LONG).show();

     wl.release();         

}
public void SetAlarm(Context context)
{
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi); 
}

public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}
public void setOnetime(Context context){
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.TRUE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager
4

1 回答 1

0

使用此方法每 5 秒获取一次位置

private final FIVE_SECONDS = 5000;
public void scheduleSendLocation() {
    handler.postDelayed(new Runnable() {
        public void run() {
            sendLocation();          // this method will contain your almost-finished HTTP calls
            handler.postDelayed(this, FIVE_SECONDS);
        }
    }, FIVE_SECONDS);
}
于 2013-08-07T11:06:35.377 回答