Android - 无法获取gps位置在真实设备中

时间:2016-10-17 09:58:00

标签: android android-studio-2.2 android-gps

正在使用 Android Studio 2.2.1 ,用户sdk - min = " 18" 最大 = " 24"
使用的许可

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

错误 - 通话需要获得用户拒绝的许可
  错误行

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);

我的完整代码在这里声明。谢谢

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener  {

    private LocationManager locationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R. layout.activity_main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);
    }

    @Override

    public void onLocationChanged(Location location) {
        String msg = "New Latitude: " + location.getLatitude()
                + "New Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Toast.makeText(getBaseContext(), "Gps is STATUS CHANGED!! ",
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String s) {
        Toast.makeText(getBaseContext(), "Gps is turned on!! ",
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String s) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "Gps is turned off!! ",
                Toast.LENGTH_SHORT).show();

    }
}

3 个答案:

答案 0 :(得分:0)

使用NETWORK_PROVIDER获取位置,因为很多时候GPS会给出null

 m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    m_location = m_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

在清单

中添加以下权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

答案 1 :(得分:0)

您需要为Android Marshmallow提供运行时权限。

官方指南:Click here

其他帮助:Click here

这是一些编码指南。

在你的onCreate方法中:

if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(SplashActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);

} else{
   //   getLocationfromYourDevice();
}

同时添加此代码以获取授予/拒绝权限的结果。

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this,"GPS permission granted",Toast.LENGTH_LONG).show();

            //  getLocationfromYourDevice();

            } else {

                 // Close the app or disable the location function

            }
            break;
    }
}

答案 2 :(得分:0)

在您的活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      if (permissionsMashmellow.hasPermissions(PERMISSIONS)) {

                GPSTracker gps = new GPSTracker(AlarmActivity.this);
                if (gps.canGetLocation()) {
                   String latitude = gps.getLatitude();
                   String longitude = gps.getLongitude();
                }
            } else {
                permissionsMashmellow.requestPermissions(PERMISSIONS, PERMISSION_ALL);
            }
}

 @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
       if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the

                GPSTracker gps = new GPSTracker(AlarmActivity.this);
                if (gps.canGetLocation()) {
                   String latitude = gps.getLatitude();
                   String longitude = gps.getLongitude();
                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
    }

添加以下代码以请求权限

public boolean hasPermissions(String... permissions) {
    if(VERSION.SDK_INT >= 23 && this.mContext != null && permissions != null) {
        String[] var2 = permissions;
        int var3 = permissions.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String permission = var2[var4];
            if(ActivityCompat.checkSelfPermission(this.mContext, permission) != 0) {
                return false;
            }
        }
    }

    return true;
}

public void requestPermissions(String[] PERMISSIONS, int PERMISSION_ALL) {
    ActivityCompat.requestPermissions((Activity)this.mContext, PERMISSIONS, PERMISSION_ALL);
}

GpsTracker.class获取当前的地址