5

私のアプリのシナリオは、従業員の場所を追跡したいというものです。デバイスのブートブロードキャストをリッスンし、アラームマネージャを登録するブロードキャストレシーバーがあります。アラームマネージャがチェックすると、2つのロケーションリスナーを登録します。1つはGPSをリッスンし、もう1つはネットワークをリッスンします。onLocationChange()メソッドで最初の場所の更新を取得したときに、場所を保存し、その場所のリスナーの登録を解除して、アラームマネージャーが再度チェックしたときに重複しないようにします。登録を解除するために、onLocationChange()でそれらにアクセスするための静的オブジェクトとしてロケーションリスナーがあります。しかし、ロケーションリスナーを削除していないことがわかりました。これが私のコード例です:

 public class BootTimeServiceActivator extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Calendar calendar = Calendar.getInstance();
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent mIntent = new Intent(context, MyBroadCastReceiver.class);
        PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20 * 60 * 1000, mPendingIntent);


    }

    }

//..........

    public class MyBroadCastReceiver extends BroadcastReceiver{

    public static LocationManager locationManager;
    public static MyLocationListener networkLocationListener;
    public static MyLocationListener gpsLocationListener;



    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Alarm has been called...", Toast.LENGTH_SHORT).show();
        initLocationListeners(context);
        registerLocationListeners();

    }

    public void initLocationListeners(Context context) {
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        networkLocationListener = new MyLocationListener(context);
        gpsLocationListener = new MyLocationListener(context);

    }

    public void registerLocationListeners() {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsLocationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, gpsLocationListener);

    }

}

\\.....

    public class MyLocationListener implements LocationListener {

    Context context;

    public MyLocationListener(Context context) {
        this.context = context;
    }

    @Override
    public void onLocationChanged(Location location) {
        if(location != null) {
            SDCardService sdService = new SDCardService(context);
            try {
                sdService.logToDB(location);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("provider",location.getProvider());
            if(location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.gpsLocationListener);
            }
            else if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
                MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.networkLocationListener);
            }



        }


    }

誰かが私が間違っているところを教えてもらえますか?

4

3 に答える 3

1

これを試して..

lmNetwork.removeUpdates(networkLocationListener);
lmGps.removeUpdates(gpsLocationListener);
于 2012-09-17T11:51:29.490 に答える
1

ああ… タイプミスがあります。実際には MyBroadCastREciever クラスで、 gpsListener を次のように 2 回登録していました。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
    100, 0, 
    gpsLocationListener);

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
    100, 0, 
    networkLocationListener); //previously was gpsLocationListener again. Wrong.

つまり、ネットワーク リスナーは削除されませんでした。

于 2012-09-17T12:20:51.740 に答える
0

インサイダー クラスを使用してこの方法で実行し、リスナー onPause() を削除できます。

        @Override
        protected void onPause() {
                super.onPause();
                GPSlocationManager.removeUpdates(GPSLocationListener);
                CellLOcationManager.removeUpdates(NetworkLocationListener);
        }

        private void SetLocationManager() {
                GPSlocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
                CellLOcationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                NetworkLocationListener = new MyNetworkLocationListener();
                GPSLocationListener = new MyGPSLocationListener();
                GPSlocationManager.requestLocationUpdates("gps", 5000, 1, GPSLocationListener);
                CellLOcationManager.requestLocationUpdates("network", 10000, 1, NetworkLocationListener);
        }

        private class MyNetworkLocationListener implements LocationListener {

                @Override
                public void onLocationChanged(Location location) {

                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {
                }

                @Override
                public void onProviderEnabled(String s) {
                }

                @Override
                public void onProviderDisabled(String s) {
                }

        }
于 2015-08-02T21:07:37.050 に答える