Android位置服务

时间:2015-08-25 17:12:00

标签: android

分辨

建议采用单身模式。解决问题不是最优雅的方式,但它有效。这是我使用的代码。只需要在main和LocationService类中获取实例即可使用。请参阅答案,了解您的假设。

import android.location.Location;

public class LocationSingleton {
    private Location location;

    private static LocationSingleton singleton = new LocationSingleton();
    private LocationSingleton(){
    }

    public static LocationSingleton getInstance( ) {
        return singleton;
    }

    protected void setLocation(Location newLocation)  {
        this.location = newLocation;
    }

    protected Location getLocation(){
        return this.location;
    }
}

<小时/> 尝试使用后台服务来处理位置更新。我在Main中创建了一个更新数据的服务(我相信工作正常)但是我还有一个main按钮,每当按下一些按钮就可以处理位置数据。我无法弄清楚如何将位置从服务传递回main。我已经尝试将Main传递给服务但是这不起作用并且始终使用没有main传递的构造函数创建服务。我也尝试在服务中创建一个返回位置但仍然获得空指针异常的方法。如何做到这一点。

服务类

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationListener;

import java.util.Locale;

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private String TAG = "LocationService";
    private GoogleApiClient mGoogleApiClient;
    private static final long INTERVAL = 1000 * 15;
    private static final long FATEST_INTERVAL = 1000 * 30;
    private LocationRequest mLocationRequest;
    private Location mCurrentLocation;
    private Geocoder geocoder;
    AddressStringOperations addressOps;
    TimerUpdate timerUpdate;
    Context mainContext;
    MainActivity act;

    public LocationService(MainActivity act) {
        this.mainContext = mainContext;
        Log.e(TAG, "Correct Constructor");
    }

    public LocationService(){
        Log.e(TAG, "Shouldn't use");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate");

        this.geocoder = new Geocoder(this, Locale.getDefault());
        addressOps = new AddressStringOperations(this.geocoder);
        this.timerUpdate = new TimerUpdate(this, addressOps);
        timerUpdate.startTimer();

        mGoogleApiClient = new GoogleApiClient.Builder(LocationService.this)
                .addApi(LocationServices.API).addConnectionCallbacks(LocationService.this)
                .addOnConnectionFailedListener(LocationService.this).build();

        mGoogleApiClient.connect();
        createLocationRequest();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Log.e(TAG, "Service Started");
        return super.onStartCommand(intent, flags, startId);
    }

    protected void createLocationRequest(){
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FATEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    public Location getLocation(){
        Log.e(TAG, "Latitude: "+ mCurrentLocation.getLatitude() + "\n" + "Longitude: " + mCurrentLocation.getLatitude());
        return this.mCurrentLocation;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.e(TAG, "Connection Successful");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.e(TAG, "Connection Lost");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(TAG, "Connection Failed");
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.e(TAG, "Firing onLocationChanged.........");
        //Log.e(TAG, "Latitude: "+ location.getLatitude() + "\n" + "Longitude: " + location.getLatitude());
        timerUpdate.location = location;
        mCurrentLocation = location;
    }
}

主类

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.location.Geocoder;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import java.util.Locale;

public class MainActivity extends Activity {

    private Button bLogout, bWebsite;
    private ImageButton bLogData;
    private TextView etLabel;
    private UserLocalStore userLocalStore;
    private static final String TAG = "MainActivity";
    TimerUpdate timerUpdate;
    Geocoder geocoder;
    Location location;
    AddressStringOperations addressOps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "On Create . . . . .");

        if(!isGooglePlayServicesAvailable()){
            startActivity(new Intent(MainActivity.this, login.class));
            Log.e(TAG, "No GooglePlayServices");
            finish();
            Toast.makeText(getApplicationContext(), "Please update GooglePlay Servies to use this Application", Toast.LENGTH_LONG).show();
        }else{
            userLocalStore = new UserLocalStore(this);
            this.geocoder = new Geocoder(this, Locale.getDefault());
            addressOps = new AddressStringOperations(this.geocoder);

            this.timerUpdate = new TimerUpdate(this, addressOps, false);

            if (authenticate() != true) {
                startActivity(new Intent(MainActivity.this, login.class));
                Log.e(TAG, "Authenticate is not true");
                finish();
            } else {
                etLabel = (TextView) findViewById(R.id.etEmailLabel);
                bLogout = (Button) findViewById(R.id.bLogout);
                bLogData = (ImageButton) findViewById(R.id.DataLog);
                bWebsite = (Button) findViewById(R.id.website);

                LocationService service = new LocationService(MainActivity.this);
                Intent start = new Intent(MainActivity.this, LocationService.class);
                MainActivity.this.startService(start);

                bLogData.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        String pressStatus = "3";
                        Log.e(TAG, "Latitude: "+ location.getLatitude() + "\n" + "Longitude: " + location.getLatitude());
                        timerUpdate.update(pressStatus);
                    }
                });

                bLogout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        userLocalStore.clearuserData();
                        userLocalStore.setUserLoggedIn(false);
                        startActivity(new Intent(MainActivity.this, login.class));
                        finish();
                    }
                });

                bWebsite.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("temp"));
                        startActivity(browserIntent);
                    }
                });
            }
        }
    }

    public void setLocation(Location newLocation){
        this.location = newLocation;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过多种方式实现这一目标。您可以从那里创建接收器和广播和意图,或者您可以使用EventBus(https://github.com/greenrobot/EventBus)。

在这种情况下,我认为服务/接收器模型更合适,您可以在此处找到一个示例:http://www.truiton.com/2014/09/android-service-broadcastreceiver-example/

如果只在一个活动中需要它,可以考虑使用AsyncTask并在postExecute方法中处理输出,这样会容易得多。