Android studio 028 获取GPS

第一步权限 两部分一部分是手机中要设置定位权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mylocation20">

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

第二步布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/provider"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

第三步主代码
package com.example.mylocation20;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView textView;

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

        textView = findViewById(R.id.provider);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

//        List providerName=locationManager.getAllProviders();
//        StringBuffer stringBuffer=new StringBuffer();
//        for(Iterator iterator=providerName.iterator();iterator.hasNext();){
//            stringBuffer.append(iterator.next()+"\n");
//        }
//
//        textView.setText((stringBuffer.toString()));
//第二中方法
//        LocationProvider locationPovider=locationManager.getProvider(LocationManager.GPS_PROVIDER);
//        textView.setText(locationPovider.getName());
//手机设置权限可以获取位置信息。必须。否则程序关闭。
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.ACCURACY_LOW);
        String provider = locationManager.getBestProvider(criteria, true);
        textView.setText(provider);

        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, 1000, 1, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        });
        Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        locationUpdates(location);
    }

    public void locationUpdates(Location location){
        if(location!=null){
            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("my xx location.\n");
            stringBuffer.append("jingdu");
            stringBuffer.append(location.getLongitude());
            stringBuffer.append("weidu");
            stringBuffer.append(location.getLatitude());
            textView.setText(stringBuffer.toString());
        }else {
            textView.setText("no.......");
        }

    }
}

Android studio 028 获取GPS_第1张图片

Android studio 028 获取GPS_第2张图片

你可能感兴趣的:(Android,studio,代码)