位置不适用于真实设备

时间:2016-02-04 14:14:39

标签: android google-maps gps

我想通过toast显示带有纬度和经度信息的消息,所以我在mainActivity中编写了这段代码:

public class RunActivity extends AppCompatActivity implements LocationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_run);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(getApplicationContext(), "Latitude : "+location.getLatitude()+", Longitude : "+location.getLongitude(), Toast.LENGTH_LONG);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(getApplicationContext(), provider+" is enabled", Toast.LENGTH_LONG);
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(), provider+" is disabled", Toast.LENGTH_LONG);
}

}

我直接在我的设备(三星S5)上启动它,但不会显示吐司。有任何想法吗?

2 个答案:

答案 0 :(得分:0)

在Android Marshmallow中,用户必须在运行时授予权限(而不是像之前一样安装时)。在您的代码段中,处理该权限请求的代码为空

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

        return;
    }

所以在requestLocationUpdates之前调用return。也就是说,为什么不调用它。

快速修复:转到智能手机设置。转到应用,查找您的应用,并手动授予权限。

从长远来看,您需要实现代码中的TODO。请参阅google developers guide

修改 我刚刚在这里看到了你真正的问题......你只是忘了在那个吐司上打电话给show()

Toast.makeText().show();

答案 1 :(得分:0)

我设法通过基于我在书中获得的一些信息创建另一个项目来找到解决方案,所以我只是在这里发布我的代码,希望它能帮助某人。它完全是功能性的:

public class MainActivity extends AppCompatActivity {

public TextView Long;
public TextView Lat;
public TextView Speed;
public TextView Alt;
public TextView Clock;
public TextView Provider;
public String ProviderName;

private final LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        setContentView(R.layout.activity_main);
        Provider = (TextView) findViewById(R.id.Provider);
        Long = (TextView) findViewById(R.id.Lon);
        Lat = (TextView) findViewById(R.id.Lat);
        Speed = (TextView) findViewById(R.id.Speed);
        Alt = (TextView) findViewById(R.id.Alt);
        Clock = (TextView) findViewById(R.id.Time);
        Provider.setText(ProviderName);
        Long.setText(Double.toString(location.getLongitude()));
        Lat.setText(Double.toString(location.getLatitude()));
        Speed.setText(Float.toString(location.getSpeed()));
        Alt.setText(Double.toString(location.getAltitude()));
        Date date = new Date(location.getTime());
        DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
        Clock.setText(dateFormat.format(date));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), provider + " is enabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), provider + " is disabled", Toast.LENGTH_LONG).show();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationProvider provider = lm.getProvider("gps");
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    ProviderName = provider.getName();


}

}

感谢。