Android 如何使用AlarmManager停止GPS更新(RemoveUpdate)

Android 如何使用AlarmManager停止GPS更新(RemoveUpdate),android,locationmanager,Android,Locationmanager,我正在开发一个应用程序,用户可以设置一个特定的时间范围,比如说一天中的一小时,我想用GPS来监控他们的位置。这基本上类似于基于位置的提醒应用程序,但有特定的用途。 我的问题是如何在指定时间启动和停止GPS。目前,我正在使用AlarmManager使用开始/结束时间创建广播。这个广播带我到MyBroadcastReceiver类。问题是RemoveUpdate没有停止GPS。我想这是因为每次调用广播接收器时,我都在创建一个新的LocationManager。因此,对于代码中的停止状态,我只是将Re

我正在开发一个应用程序,用户可以设置一个特定的时间范围,比如说一天中的一小时,我想用GPS来监控他们的位置。这基本上类似于基于位置的提醒应用程序,但有特定的用途。 我的问题是如何在指定时间启动和停止GPS。目前,我正在使用AlarmManager使用开始/结束时间创建广播。这个广播带我到MyBroadcastReceiver类。问题是RemoveUpdate没有停止GPS。我想这是因为每次调用广播接收器时,我都在创建一个新的LocationManager。因此,对于代码中的停止状态,我只是将RemoveUpdate发送到最近创建的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

    }

}

事实上,别介意这个问题。我决定创建一个后台服务来处理这个问题。这种方法似乎进展顺利得多