使用AndroidAPI提供的LocationManager進行定位,獲取經緯度
前臺.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_2"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_3"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doClick" android:text="Listen"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="disClick" android:text="UnListen"/> </LinearLayout> </LinearLayout>
PermissionUtils.java工具類(判斷是否有權限,動態獲取權限)
import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import androidx.annotation.NonNull; import androidx.annotation.Size; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.List; public class PermissionUtils { // 判斷是否有權限 public static boolean hasPermissions(@NonNull Context context, @Size(min = 1) @NonNull String... perms) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return true; } if (context == null) { throw new IllegalArgumentException("Can't check permissions for null context"); } for (String perm : perms) { if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } //申請權限 public static void requestPermissions(@NonNull Activity activity, int requestCode, String[] permissions) { List<String> permissionList = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) { permissionList.add(permission); } } String[] permissionsArray = permissionList.toArray(new String[permissionList.size()]);//將List轉爲數組 if (permissionList.isEmpty()) { //不可能爲空 } else { ActivityCompat.requestPermissions(activity, permissionsArray, requestCode); //返回結果onRequestPermissionsResult } } }
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private LocationManager mLocationManager; private TextView txt_1; private TextView txt_2; private TextView txt_3; private String[] permissions = { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }; private static final int REQUEST_PERMISSION_CODE = 12;//定位權限 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void doClick(View view) { if (!PermissionUtils.hasPermissions(this, permissions)) { PermissionUtils.requestPermissions(this, REQUEST_PERMISSION_CODE, permissions); } else { startLocate(); } } public void disClick(View view) { mLocationManager.removeUpdates(locationListener); cleanText(); } public void init() { txt_1=findViewById(R.id.txt_1); txt_2=findViewById(R.id.txt_2); txt_3=findViewById(R.id.txt_3); } @SuppressLint("MissingPermission") private void startLocate() { mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);//獲取定位權限 boolean providerEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);//isProviderEnabled函數返回一個bool類型,判斷當前參數請求的權限是否打開 if (providerEnabled) { /* * par1:定位類型,這裏使用的是GPS * par2:更新週期(ms) * par3:更新最小範圍(cm) * par4:監聽委託(使用匿名函數進行邏輯處理) * */ mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } else { Toast.makeText(this, "請打開GPS", Toast.LENGTH_SHORT).show(); } } private LocationListener locationListener = new LocationListener() { @SuppressLint("SetTextI18n") @Override public void onLocationChanged(Location location) { //位置信息變化時觸發 txt_1.setText("定位方式:" + location.getProvider()); txt_2.setText("緯度:" + location.getLatitude()); txt_3.setText("經度:" + location.getLongitude()); } }; @SuppressLint("SetTextI18n") private void cleanText() { txt_1.setText("定位方式:" +"???"); txt_2.setText("緯度:" +"0.0"); txt_3.setText("經度:" +"0.0"); } }
在AndroidManifest.xml中添加權限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
結束,看一下運行效果
點擊Listen按鈕以後進行獲取
java