Android GPS不适合我

时间:2015-04-21 12:03:57

标签: android gps

我的应用程序有两个需要GPS的活动,因此我尝试将其卸载到一个活动可以使用的单独类中。我在这里找到了一个看起来很容易的答案 Android - Best way to implement LocationListener across multiple activities 但是,当然,它不适合我。我想知道是否有人能看到这个问题。我使用了完全相同的代码,但是我摆脱了gps设置对话框。

这是我的GPS课程

package fieldlayout.skipmorrow.com.fieldlayout;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;

import android.location.LocationListener;

import android.location.Location;
import android.util.Log;


/**
 * Created by skip on 4/20/2015.
 */
public class GPS {
    private IGPSActivity main;

    // Helper for GPS-Position
    private LocationListener mlocListener;
    private LocationManager mlocManager;

    private boolean isRunning;

    public GPS(IGPSActivity main) {
        this.main = main;

        // GPS Position
        mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        // GPS Position END
        this.isRunning = true;
        Log.i("FieldLayout_GPS", "GPS Object created");
    }

    public void stopGPS() {
        if(isRunning) {
            mlocManager.removeUpdates(mlocListener);
            this.isRunning = false;
        }
        Log.i("FieldLayout_GPS", "stopGPS");
    }

    public void resumeGPS() {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        this.isRunning = true;
        Log.i("FieldLayout_GPS", "resumeGPS");
    }

    public boolean isRunning() {
        return this.isRunning;
    }

    public class MyLocationListener implements LocationListener {

        private final String TAG = MyLocationListener.class.getSimpleName();

        @Override
        public void onLocationChanged(Location loc) {
            GPS.this.main.locationChanged(loc.getLongitude(), loc.getLatitude());
            Log.i("FieldLayout_GPS", "onLocationChanged");
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i("FieldLayout_GPS", "onStatusChanged");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.i("FieldLayout_GPS", "onProviderEnabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i("FieldLayout_GPS", "onProviderDisabled");
        }
    }

}

接口文件

package fieldlayout.skipmorrow.com.fieldlayout;

/**
 * Created by skip on 4/20/2015.
 */
public interface IGPSActivity {
    public void locationChanged(double longitude, double latitude);
}

我的活动实施

package fieldlayout.skipmorrow.com.fieldlayout;

import android.content.Context;
import android.content.Intent;


public class StartActivity extends ActionBarActivity implements IGPSActivity{

    private Location currentLocation;
    private GPS gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        gps = new GPS(this);
    }

    @Override
    protected void onResume() {
        if (!gps.isRunning()) gps.resumeGPS();
        super.onResume();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        Log.i("FieldLayout_StartAct", "onStop called. Disconnecting GPS client");
        gps.stopGPS();
        super.onStop();
    }

    @Override
    public void locationChanged(double longitude, double latitude) {
        Log.i("FieldLayout_StartAct", "locationChanged");
        currentLocation.setLatitude(latitude);
        currentLocation.setLongitude(longitude);
    }
}

我看到的唯一日志是创建GPS对象。侦听器中没有其他方法正在执行。

1 个答案:

答案 0 :(得分:1)

GPS类构造函数包含以下行:

 public GPS(IGPSActivity main) {
    this.main = main;

    // GPS Position
    mlocManager = (LocationManager) ((Activity) this.main).getSystemService(Context.LOCATION_SERVICE);
...
}

您的界面如下所示:

public interface IGPSActivity {
    public void locationChanged(double longitude, double latitude);

}

因此,GPS类获得一个接口,并且您希望将其转换为Activity以从中获取系统服务。那不行。

稍微改变您的GPS等级。例如:

public GPS(IGPSActivity main, Activity activity){
    mlocManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
...
}

在您的活动中调用此方法:

gps = new GPS(this, this);