weixin_44061521
血莲丹
2019-04-22 17:03

使用Android studio实现简单的GPS定位,为什么真机上不显示定位信息?

  • android-studio
  • android

布局界面
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的定位信息:"
/>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv1"
android:text="xinxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

MainActivity.java界面:
package com.example.gps3;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView textView;
LocationManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.tv1);
    manager = (LocationManager) getSystemService(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;
    }

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    });
}
public void updateLocation(Location location){
    textView.setText("");
    textView.append("经度是:"+location.getLongitude());
    textView.append("\n纬度是:"+location.getLatitude());
    textView.append("\n高度:"+location.getAltitude());
    textView.append("\n方向:"+location.getBearing());
    textView.append("\n速度:"+location.getSpeed());
}

}

为什么我的第二个TextView不显示定位信息?
谢谢,第一次提问,排版不好,还请见谅!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答