【发布时间】:2011-12-08 21:39:56
【问题描述】:
我正在开发一个用户设置指定时间范围的应用程序,比如一天中的一个小时,我想使用 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
}
}
【问题讨论】: