如何使用android中的报警管理器每五秒显示经度和纬度?

时间:2013-08-07 09:28:04

标签: android

我正在创建一个应用程序,我必须每五分钟显示一次提醒。而不是提醒我必须每五分钟显示一次经度和纬度。我有两项活动:一项是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

1 个答案:

答案 0 :(得分: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);
}