LocationListener 不提供任何速度更新
LocationListener not giving any speed updates
我正在尝试使用 LocationListener
获取我的 GPS 速度。
我在第一次启动应用程序时请求许可,然后获得许可,"onLocationChanged()"立即显示祝酒词,但再也不会。
我在 phone 上激活了定位功能。
但是我没有收到任何位置更新。
很抱歉转储了我的所有代码,但我不确定要显示什么。
有人知道我在这里遗漏了什么吗?
package com.example.username.yaddayadda;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.Location;
import android.widget.Toast;
import java.util.Formatter;
import java.util.Locale;
@TargetApi(23)
public class MainActivity extends Activity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
if (checkIfPermissionIsGranted()){
Toast toast = Toast.makeText(getApplicationContext(), "Permission is granted for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
1);
if(checkIfPermissionIsGranted()){
Toast toast = Toast.makeText(getApplicationContext(), "Permission was granted for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Permission was denied for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
}
}
} catch(SecurityException e){
Context context = getApplicationContext();
CharSequence text = "Got exception: \n" + e.getMessage();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
this.onLocationChanged(null);
}
private boolean checkIfPermissionIsGranted(){
return (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
@Override
public void onLocationChanged(Location location) {
TextView txt = (TextView) this.findViewById(R.id.txtCurrentSpeed);
Toast toast = Toast.makeText(getApplicationContext(), "onLocationChanged()", Toast.LENGTH_SHORT);
toast.show();
if(location==null){
txt.setText("-.- m/s");
}
else {
if(location.hasSpeed()){
float nCurrentSpeed = location.getSpeed();
txt.setText(nCurrentSpeed + " m/s");
} else {
txt.setText("0.0 m/s");
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast toast = Toast.makeText(getApplicationContext(), "onStatusChanged()", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onProviderEnabled(String provider) {
Toast toast = Toast.makeText(getApplicationContext(), "onProviderEnabled()", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast toast = Toast.makeText(getApplicationContext(), "onProviderdisabled()", Toast.LENGTH_SHORT);
toast.show();
}
}
"onLocationChanged()" shows a toast immediately but never again.
您没有收到任何位置更新,但您在此处自行调用此函数
// toast because of this , so no need of this , remove this call
this.onLocationChanged(null);
But I don't get any more location updates.
因为第一次,您拥有权限,然后一旦获得权限,您需要再次从 onRequestPermissionsResult
.
执行位置请求代码
所以你需要覆盖 onRequestPermissionsResult
并且一旦你获得了权限然后 requestLocationUpdates
再次像
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Request location updates:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000*60, this);
}
} else {
// permission denied, Disable the
// functionality that depends on this permission.
}
return;
}
}
}
注意:最好向两个提供商请求位置,只需使用最好的一个,您可以将您的位置请求代码移动到单独的函数中,并在需要时调用它。
引用
我正在尝试使用 LocationListener
获取我的 GPS 速度。
我在第一次启动应用程序时请求许可,然后获得许可,"onLocationChanged()"立即显示祝酒词,但再也不会。
我在 phone 上激活了定位功能。 但是我没有收到任何位置更新。
很抱歉转储了我的所有代码,但我不确定要显示什么。
有人知道我在这里遗漏了什么吗?
package com.example.username.yaddayadda;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.Location;
import android.widget.Toast;
import java.util.Formatter;
import java.util.Locale;
@TargetApi(23)
public class MainActivity extends Activity implements LocationListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
if (checkIfPermissionIsGranted()){
Toast toast = Toast.makeText(getApplicationContext(), "Permission is granted for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
1);
if(checkIfPermissionIsGranted()){
Toast toast = Toast.makeText(getApplicationContext(), "Permission was granted for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Permission was denied for fine and coarse location", Toast.LENGTH_LONG);
toast.show();
}
}
} catch(SecurityException e){
Context context = getApplicationContext();
CharSequence text = "Got exception: \n" + e.getMessage();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
this.onLocationChanged(null);
}
private boolean checkIfPermissionIsGranted(){
return (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
}
@Override
public void onLocationChanged(Location location) {
TextView txt = (TextView) this.findViewById(R.id.txtCurrentSpeed);
Toast toast = Toast.makeText(getApplicationContext(), "onLocationChanged()", Toast.LENGTH_SHORT);
toast.show();
if(location==null){
txt.setText("-.- m/s");
}
else {
if(location.hasSpeed()){
float nCurrentSpeed = location.getSpeed();
txt.setText(nCurrentSpeed + " m/s");
} else {
txt.setText("0.0 m/s");
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast toast = Toast.makeText(getApplicationContext(), "onStatusChanged()", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onProviderEnabled(String provider) {
Toast toast = Toast.makeText(getApplicationContext(), "onProviderEnabled()", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onProviderDisabled(String provider) {
Toast toast = Toast.makeText(getApplicationContext(), "onProviderdisabled()", Toast.LENGTH_SHORT);
toast.show();
}
}
"onLocationChanged()" shows a toast immediately but never again.
您没有收到任何位置更新,但您在此处自行调用此函数
// toast because of this , so no need of this , remove this call
this.onLocationChanged(null);
But I don't get any more location updates.
因为第一次,您拥有权限,然后一旦获得权限,您需要再次从 onRequestPermissionsResult
.
所以你需要覆盖 onRequestPermissionsResult
并且一旦你获得了权限然后 requestLocationUpdates
再次像
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Request location updates:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000*60, this);
}
} else {
// permission denied, Disable the
// functionality that depends on this permission.
}
return;
}
}
}
注意:最好向两个提供商请求位置,只需使用最好的一个,您可以将您的位置请求代码移动到单独的函数中,并在需要时调用它。
引用