简体   繁体   English

无法通过GoogleApiClient使用GPS获取当前位置

[英]Unable to get current location using GPS with GoogleApiClient

I am trying to make a button work in my application that will collect the latitude and longitude of the device when the user clicks it. 我正在尝试使我的应用程序中的按钮起作用,该按钮将在用户单击设备时收集设备的纬度和经度。 The button works fine, yet it is not calling the class method from my GPSHelper class. 该按钮工作正常,但未从我的GPSHelper类中调用该类方法。 Any help with this would be appreciated. 任何帮助,将不胜感激。 I am testing and trying this out on my actual device and not an emulator if that helps any. 我正在测试并在我的实际设备上进行测试,而不是仿真器(如果有帮助的话)。 Thanks for any help. 谢谢你的帮助。

Here is my GPSHelper class where I am placing all the permissions checks and code for the GPS usage. 这是我的GPSHelper类,我将在其中放置所有权限检查和GPS使用情况的代码。

import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;


/**
 * This class takes care of capturing the location of the device.
 */

public class GPSHelper extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, ActivityCompat.OnRequestPermissionsResultCallback{

    private final Context mContext;
    boolean isGpsEnabled = false;
    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    double mLatitude;
    double mLongitude;

    public GPSHelper(Context context) {
        this.mContext = context;
    }

    public Location getLocation() {
        mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
        isGpsEnabled = mGoogleApiClient.isConnected();

        if (isGpsEnabled){
            mLatitude = mLastLocation.getLatitude();
            mLongitude = mLastLocation.getLongitude();
            Toast.makeText(this, String.valueOf(mLatitude), Toast.LENGTH_LONG).show();
            Toast.makeText(this, String.valueOf(mLongitude), Toast.LENGTH_LONG).show();
            mGoogleApiClient.disconnect();
        }

        return mLastLocation;
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.mContext, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) this.mContext,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.INTERNET}, 10);
            }
        } else {
            if (mLastLocation == null){
                mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                getLocation();
            }else {
                getLocation();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 10:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    getLocation();
                }else {
                    Toast.makeText(this, "Action Canceled\nGPS Is Required",Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }

    }

    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

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

}

Here is the activity where I am trying to call the getLocation() class method with an instance of the GPSHelper class. 这是我尝试使用GPSHelper类的实例调用getLocation()类方法的GPSHelper

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.bigdaddy.as_built_weldmapper.utilities.BendHelper;
import com.example.bigdaddy.as_built_weldmapper.utilities.GPSHelper;

public class SagActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,
        MajorButtonFragment.OnFragmentInteractionListener, Communicator{

    /* Using this to insert into the Bend Direction field. */
    public static String SAG_DIRECTION = "SAG";

    /* This spinner holds the bend types */
    Spinner mSagBendTypesSpinner;

    /* Using this string to collect what was selected for the spinner type */
    private String mBendTypeSpinnerVal;

    /* All the EditText for the Activity */
    private EditText mSagGpsShotEt;
    private EditText mSagExistingGpsEt;
    private EditText mSagCoverEt;
    private EditText mSagDegreeEt;
    private EditText mSagDistanceFromEt;
    private EditText mSagNotesEt;
    private EditText mSagOccupyIdEt;
    private EditText mSagStationNumEt;

    public GPSHelper mGPSHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sag);
        mGPSHelper = new GPSHelper(SagActivity.this);

        /* checking if the MajorButtonFragment is null */
        if (findViewById(R.id.majorButtonFragment) != null) {
            if (savedInstanceState != null) {
                return;
            }
        }

        /* Referencing the spinner and setting the itemsSelectedListener */
        mSagBendTypesSpinner = (Spinner) findViewById(R.id.bend_types_spinner);
        mSagBendTypesSpinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
        /* Create an ArrayAdapter using the string array and a default spinner layout */
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.bend_types_array, android.R.layout.simple_spinner_item);
        /* Specify the layout to use when the list of choices appears */
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        /* Apply the adapter to the spinner */
        mSagBendTypesSpinner.setAdapter(adapter);

        /* Referencing and calling all the EditText for the Activity */
        mSagGpsShotEt = (EditText) findViewById(R.id.eTextGpsShotForSag);
        mSagExistingGpsEt = (EditText) findViewById(R.id.eTextExistGradeForSag);
        mSagCoverEt = (EditText) findViewById(R.id.eTextCoverForSag);
        mSagDegreeEt = (EditText) findViewById(R.id.eTextDegreeForSag);
        mSagDistanceFromEt = (EditText) findViewById(R.id.eTextDistanceFromForSag);
        mSagNotesEt = (EditText) findViewById(R.id.eTextNotesForSagActivity);
        mSagOccupyIdEt = (EditText) findViewById(R.id.eTextJointIdSagActivity);
        mSagStationNumEt = (EditText) findViewById(R.id.eTextStationNumSagActivity);
    } /*onCreate() ends here.*/

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        mBendTypeSpinnerVal = mSagBendTypesSpinner.getSelectedItem().toString();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    @Override
    public void exitBtnRespond() {

    }

    /**
     * This overridden method comes from the Communicator Interface and is used globally in all
     * Activities that implement it to Store (write) a transaction to the database.
     * The utility class saveAndInsertBend() method is invoked here.
     */
    @Override
    public void storeBtnRespond() {
        BendHelper.saveAndInsertBend(SagActivity.this, SAG_DIRECTION, mBendTypeSpinnerVal, mSagStationNumEt,
                mSagOccupyIdEt, mSagDegreeEt, mSagDistanceFromEt, mSagGpsShotEt, mSagExistingGpsEt,
                mSagCoverEt, mSagNotesEt);
    }

    @Override
    public void mapItBtnRespond() {
        Toast.makeText(this, "MapItBtn clicked in SagActivity",Toast.LENGTH_LONG).show();
        mGPSHelper.getLocation();

    }

    @Override
    public void onFragmentInteraction() {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }
} 

The Overridden mapItBtnRespond() method comes from a Fragment class I have that contains my buttons and it does work. mapItBtnRespond()方法来自我有一个Fragment类,它包含我的按钮,并且确实起作用。 The Toast message displays every time I click the button but no permissions are asked for on nothing. 每次我单击按钮时,都会显示Toast消息,但没有任何要求的权限。 Where am I going wrong with all of this? 所有这些我哪里出错了?

Also here is the Manifest file with the permissions in it. 同样是其中包含权限的Manifest文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.bigdaddy.as_built_weldmapper">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <!--android:theme="@style/AppTheme"> '-->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".WeldActivity">
        </activity>
        <activity android:name=".SagActivity">
        </activity>
        <activity android:name=".OverbendActivity">
        </activity>
        <activity android:name=".LtPiActivity">
        </activity>
        <activity android:name=".RtPiActivity">
        </activity>
        <activity android:name=".CmboObRtActivity">
        </activity>
        <activity android:name=".ComboObLtActivity">
        </activity>
        <activity android:name=".ComboSagRtActivity">
        </activity>
        <activity android:name=".ComboSagLtActivity">
        </activity>

    </application>

</manifest>

Thank you for any help. 感谢您的任何帮助。

This is how i am getting continues location updates 这就是我获取继续位置更新的方式

MyLocation.java MyLocation.java

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import java.util.Timer;
import java.util.TimerTask;

/**
  * Created by Rana Shahzaib on 1/9/2017.
  */

public class MyLocation {
Context context;

MyLocation(Context context) {
    this.context = context;
}

Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled = false;
boolean network_enabled = false;

public boolean getLocation(Context context, LocationResult result) {
    //I use LocationResult callback class to pass location value from MyLocation to user code.
    locationResult = result;
    if (lm == null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    //exceptions will be thrown if provider is not permitted.
    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    //Toast.makeText(context, gps_enabled+" "+network_enabled,     Toast.LENGTH_LONG).show();

    //don't start listeners if no provider is enabled
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    if (!gps_enabled && !network_enabled)
        return false;
    if (gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    if (network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
    timer1 = new Timer();


    timer1.schedule(new GetLastLocation(), 10000);
    return true;
}

LocationListener locationListenerGps = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

class GetLastLocation extends TimerTask {
    @Override

    public void run() {

        //Context context = getClass().getgetApplicationContext();
        Location net_loc = null, gps_loc = null;
        if (gps_enabled)
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(network_enabled)
            net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        //if there are both values use the latest one
        if(gps_loc!=null && net_loc!=null){
            if(gps_loc.getTime()>net_loc.getTime())
                locationResult.gotLocation(gps_loc);
            else
                locationResult.gotLocation(net_loc);
            return;
        }

        if(gps_loc!=null){
            locationResult.gotLocation(gps_loc);
            return;
        }
        if(net_loc!=null){
            locationResult.gotLocation(net_loc);
            return;
        }
        locationResult.gotLocation(null);
    }
}

public static abstract class LocationResult{
    public abstract void gotLocation(Location location);
}}

And here i am getting location 我在这里找到位置

MyLocation mLocation = new MyLocation(this);
mLocation.getLocation(this, new MyLocation.LocationResult() {

    @Override
    public void gotLocation(Location location) {
        //Your Logic
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM