该位置不起作用,我不知道为什么

时间:2018-10-20 16:07:14

标签: java android performance location

请任何人帮助我。我希望该应用程序在2个文本视图中显示我的位置,但不显示位置。 而且我想知道如何在数据库中添加位置并在需要时获取它。

感谢大家对我有帮助:)


public class Main2 extends AppCompatActivity implements LocationListener {

    private LocationManager locationManager;
    private Location location;
    private TextView txt1;
    private TextView txt2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);


        txt1 = findViewById(R.id.tv_latitude);
        txt2 = findViewById(R.id.tv_longitude);

        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;
        }


        location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
        onLocationChanged(location);


    }


    @Override
    public void onLocationChanged(Location location) {
        double longi = location.getLongitude();
        double lati = location.getLatitude();
        txt1.setText("lati:" + lati);
        txt2.setText("longi" + longi);
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }


}

清单

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

2个TextViews

    <TextView
    android:id="@+id/tv_longitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="30dp"
    android:background="@android:color/white"
    android:padding="10dp"
    android:text="long"
    android:textSize="30sp"
    android:layout_gravity="center"
    />

<TextView
    android:id="@+id/tv_latitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="30dp"
    android:background="@android:color/white"
    android:padding="10dp"
    android:text="lat"
    android:textSize="30sp"
    android:layout_gravity="center"
    />

0 个答案:

没有答案