手机上不会显示gps位置

dwbf0jvd  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(500)

我正在尝试从gps获取位置并将其显示在屏幕上,到目前为止,我有以下代码:

public class MainActivity extends AppCompatActivity implements LocationListener {

    EditText el;
    TextView output;

    LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        el = (EditText) findViewById(R.id.editText);
        output = (TextView) findViewById(R.id.textView2);
        output.setMovementMethod(new ScrollingMovementMethod());
        output.setText("");

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        output.append("Start");
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            //ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);

             output.append("Initially Failed to Grand Permission\n");
             return;
        }
        output.append("\nOutside Permission controle");

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
        output.append("\nYoo");

    }

    public void send(View v) {
        MessageSender messageSender = new MessageSender();
        messageSender.execute(el.getText().toString());
        output.append("Hello\n");

    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        output.append("Inside location function");
        output.append(String.valueOf(location.getLongitude()));
    }
}

我还在android清单中添加了精细定位和粗略定位的权限。我错过了什么?
我用的是三星galaxy s10
注意:当我在androidstudio上使用模拟器时,它会显示位置,但是当我使用usb调试器并在android设备上运行它时,它不会显示位置
而且,即使我在模拟器上运行它,当我移动笔记本电脑时,位置也不会更新,为什么?

hujrc8aj

hujrc8aj1#

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this); 如果不允许,则会导致错误 ACCESS_FINE_LOCATION 以及 ACCESS_COARSE_LOCATION 所以你必须申请这些权限。你可以这样做:

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    } else {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
    }

注意 && 已更改为 || . 基本上,这段代码会询问权限是否被授予。如果已授予权限,请调用 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this); 如果没有授予权限怎么办?您需要等到权限被授予后才能调用 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)
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1, this);
            return;
        }
    }
}

将此方法添加到活动中。我用一个物理设备测试了这段代码,它似乎正常工作。

相关问题