2

現在、Androidでのアクティビティで使用するための以下のコーディングがあります。今、私はこれをオブジェクトとして使用し、場所のスキャンを開始し、3秒後にハンドラーを使用してreturnBestLocationメソッドを使用してその場所を取得します。

ただし、MyLocationListenerオブジェクトが、3秒後にオブジェクトを元の場所に呼び出すのではなく、場所の変更時にアクティビティの呼び出しを自動的に返す可能性はありますか?

public class MyLocationListener implements LocationListener {

    LocationManager locationManager;
    Date currentBestLocationDate;
    Intent notificationIntent;
     Context mContext;
    Location currentBestLocation = null, lastKnownLocation=null;

 public MyLocationListener(Context mContext)
 {this.mContext = mContext;

 }


 public void startLocationScan()
 {
     Log.d(Config.log_id, "Custom Location Listener started");

     if (locationManager == null) {

        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        Location locationNETWORK = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (locationNETWORK != null) {
            lastKnownLocation=locationNETWORK;
        }
        Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (locationGPS != null) {
            lastKnownLocation=locationGPS;

        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,  MyLocationListener.this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,MyLocationListener.this);





    }


 }
 public void stopLocationScan()
 {

        if(locationManager!=null)
        {
            locationManager.removeUpdates(MyLocationListener.this);

             Log.d(Config.log_id, "Custom Location Listener Stopped");
        }
        }
public Location returnBestLocation()
{
    return currentBestLocation;
}

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onLocationChanged(Location location) {


        // TODO Auto-generated method stub
        if (currentBestLocation == null) {
            currentBestLocation = location;
        }

        long timeDelta = location.getTime() - currentBestLocation.getTime();
        Log.d(Config.log_id, "locationpostingservice's changed with accuracy " + location.getAccuracy() + " s different " + (float) timeDelta / 1000);

        if (timeDelta >= 120000) {
            currentBestLocation = location;
            Log.d(Config.log_id,"posting service Location changed due to over 2min "+ location.getAccuracy() + " s different "+ (float) timeDelta / 1000);

        }


        if (currentBestLocation.getAccuracy() >= location.getAccuracy()) {
            currentBestLocation = location;
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
4

2 に答える 2

4

通知するアクティビティをリスナーとしてMyLocationListenerに渡すことができます。独自のリスナーインターフェイスを作成し、アクティビティに実装させて、addListener()のようなメソッドをMyLocationListenerに追加します。これらのアクティビティに通知するたびに、リスナーのリストを繰り返し処理し、それらのlocationChangedメソッド(またはインターフェイス定義で呼び出したもの)を呼び出します。nullリスナーアクティビティなどのエラー処理を必ず追加してください。

つまり、基本的に、LocationListenerをリッスンする独自のカスタムリスナーがあります。

別の方法は、ブロードキャストレシーバーを使用して場所の変更をブロードキャストすることです。

于 2012-07-10T13:32:45.497 に答える
0

これを行う適切な方法は、Fraggleが言うように、独自のリスナーインターフェイスを作成し、それをクラスに実装してから、アクティビティからリスナーを呼び出すことです。

しかし、手っ取り早い代替手段は、新しいクラス(MyLocationListener)にスピンオフするのではなく、アクティビティにLocationListenerインターフェイスを実装することです。次に、実行するすべてのコードをonLocationChangedに配置するだけで、他のオブジェクトとの通信について心配する必要はありません。

于 2012-07-11T11:25:17.253 に答える