我正在开发一个应用程序,用户可以设置指定的时间范围,例如一天中的一小时,我想用GPS监控他们的位置。这基本上类似于基于位置的提醒应用程序,但具有特定目的。 我的问题是如何在指定时间启动和停止GPS。目前我正在使用AlarmManager使用开始/结束时间来创建广播。这个广播将我带到了“MyBroadcastReceiver”课程。问题是removeUpdates没有停止GPS。我假设这是因为每次调用广播接收器时我都会创建一个新的LocationManager。因此,在我的代码中使用“停止”状态,我只是将“removeUpdates”发送到最近创建的locationManager,而不是之前创建的locationManager。
如果AlarmManager不是完成此操作的正确方法,我愿意重新处理整个应用程序。我打算从长远来看使用用户提供的信息让每天选择的时间重复并检查GPS位置。因此,当需要更新时,我永远不会确定应用程序是否会运行。 请帮助我一直在工作和寻找好几天。
public class ASPActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startButton = (Button)findViewById(R.id.startButton);
final TimePicker startTime = (TimePicker)findViewById(R.id.startTime);
final TimePicker endTime = (TimePicker)findViewById(R.id.endTime);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar startCalendar = Calendar.getInstance();
Calendar endCalendar = Calendar.getInstance();
startCalendar.set(Calendar.HOUR_OF_DAY, startTime.getCurrentHour());
startCalendar.set(Calendar.SECOND, 0);
startCalendar.set(Calendar.MINUTE, startTime.getCurrentMinute());
endCalendar.set(Calendar.HOUR_OF_DAY, endTime.getCurrentHour());
endCalendar.set(Calendar.SECOND, 0);
endCalendar.set(Calendar.MINUTE, endTime.getCurrentMinute());
Intent intent = new Intent(ASPActivity.this, MyBroadcastReceiver.class);
Intent endIntent = new Intent(ASPActivity.this, MyBroadcastReceiver.class);
intent.putExtra("startHour", Integer.toString(startTime.getCurrentHour()));
intent.putExtra("startMinute", Integer.toString(startTime.getCurrentMinute()));
intent.putExtra("action", "start");
endIntent.putExtra("endHour", Integer.toString(endTime.getCurrentHour()));
endIntent.putExtra("endMinute", Integer.toString(endTime.getCurrentMinute()));
endIntent.putExtra("action", "stop");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
ASPActivity.this.getApplicationContext(), 234324243, intent, 0);
PendingIntent endPendingIntent = PendingIntent.getBroadcast(
ASPActivity.this.getApplicationContext(), 234324244, endIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager endAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,startCalendar.getTimeInMillis(), pendingIntent);
endAlarmManager.set(AlarmManager.RTC_WAKEUP,endCalendar.getTimeInMillis(), endPendingIntent);
} //onClick
}); //view Onclick listener
} //onCreate end
public class MyBroadcastReceiver extends BroadcastReceiver {
//Calendar mStartCalendar = Calendar.getInstance();
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
//LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//LocationListener locationListener = new MyLocationListener();
LocationManager locationManager = null;
LocationListener locationListener = null;
String action = "";
action = extras.getString("action");
System.out.println("status = " + action);
if (locationManager == null) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
}
if (action.equals("start")) {
System.out.println("Hit the start status");
//LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 1, locationListener);
} else if (action.equals("stop")) {
System.out.println("Hit the else status");
locationManager.removeUpdates(locationListener);
}
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
System.out.println("Loc Changed");
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
实际上,别介意这个问题。我决定创建一个后台服务来处理这个问题。这种方法似乎更顺畅。