权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
创建location文件夹,创建GPSLocationListener
import android.location.Location; import android.os.Bundle; public interface GPSLocationListener { /** * 方法描述:位置信息发生改变时被调用 * * @param location 更新位置后的新的Location对象 */ void UpdateLocation(Location location); /** * 方法描述:provider定位源类型变化时被调用 * * @param provider provider的类型 * @param status provider状态 * @param extras provider的一些设置参数(如高精度、低功耗等) */ void UpdateStatus(String provider, int status, Bundle extras); /** * 方法描述:GPS状态发生改变时被调用(GPS手动启动、手动关闭、GPS不在服务区、GPS占时不可用、GPS可用) * * @param gpsStatus 详见{@link GPSProviderStatus} */ void UpdateGPSProviderStatus(int gpsStatus); }
GPSLocation类
import android.location.Location; import android.location.LocationListener; import android.location.LocationProvider; import android.os.Bundle; public class GPSLocation implements LocationListener { private GPSLocationListener mGpsLocationListener; public GPSLocation(GPSLocationListener gpsLocationListener) { this.mGpsLocationListener = gpsLocationListener; } @Override public void onLocationChanged(Location location) { if (location != null) { mGpsLocationListener.UpdateLocation(location); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { mGpsLocationListener.UpdateStatus(provider, status, extras); switch (status) { case LocationProvider.AVAILABLE: mGpsLocationListener.UpdateGPSProviderStatus(GPSProviderStatus.GPS_AVAILABLE); break; case LocationProvider.OUT_OF_SERVICE: mGpsLocationListener.UpdateGPSProviderStatus(GPSProviderStatus.GPS_OUT_OF_SERVICE); break; case LocationProvider.TEMPORARILY_UNAVAILABLE: mGpsLocationListener.UpdateGPSProviderStatus(GPSProviderStatus.GPS_TEMPORARILY_UNAVAILABLE); break; } } @Override public void onProviderEnabled(String provider) { mGpsLocationListener.UpdateGPSProviderStatus(GPSProviderStatus.GPS_ENABLED); } @Override public void onProviderDisabled(String provider) { mGpsLocationListener.UpdateGPSProviderStatus(GPSProviderStatus.GPS_DISABLED); } }
管理类GPSLocationManager
import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationManager; import android.os.Build; import android.provider.Settings; import android.widget.Toast; import androidx.core.app.ActivityCompat; import java.lang.ref.WeakReference; /** * 类描述:GPS定位的管理类 */ public class GPSLocationManager { private static final String GPS_LOCATION_NAME = LocationManager.GPS_PROVIDER; private static GPSLocationManager gpsLocationManager; private static Object objLock = new Object(); private boolean isGpsEnabled; private static String mLocateType; private WeakReference<Activity> mContext; private LocationManager locationManager; private GPSLocation mGPSLocation; private boolean isOPenGps; private long mMinTime; private float mMinDistance; private GPSLocationManager(Activity context) { initData(context); } private void initData(Activity context) { this.mContext = new WeakReference<>(context); if (mContext.get() != null) { locationManager = (LocationManager) (mContext.get().getSystemService(Context.LOCATION_SERVICE)); } //type:internet mLocateType = locationManager.NETWORK_PROVIDER; isOPenGps = false; mMinTime = 1000; mMinDistance = 0; } public static GPSLocationManager getInstances(Activity context) { if (gpsLocationManager == null) { synchronized (objLock) { if (gpsLocationManager == null) { gpsLocationManager = new GPSLocationManager(context); } } } return gpsLocationManager; } /** * @param minTime */ public void setScanSpan(long minTime) { this.mMinTime = minTime; } /** * @param minDistance */ public void setMinDistance(float minDistance) { this.mMinDistance = minDistance; } /** * @param gpsLocationListener */ public void start(GPSLocationListener gpsLocationListener) { this.start(gpsLocationListener, isOPenGps); } /** * @param gpsLocationListener * @param isOpenGps */ public void start(GPSLocationListener gpsLocationListener, boolean isOpenGps) { this.isOPenGps = isOpenGps; if (mContext.get() == null) { return; } mGPSLocation = new GPSLocation(gpsLocationListener); isGpsEnabled = locationManager.isProviderEnabled(GPS_LOCATION_NAME); if (!isGpsEnabled && isOPenGps) { openGPS(); return; } if (ActivityCompat.checkSelfPermission(mContext.get(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission (mContext.get(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } Location lastKnownLocation = locationManager.getLastKnownLocation(mLocateType); mGPSLocation.onLocationChanged(lastKnownLocation); locationManager.requestLocationUpdates(mLocateType, mMinTime, mMinDistance, mGPSLocation); } public void openGPS() { Toast.makeText(mContext.get(), "please open gps", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT > 15) { Intent intent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.get().startActivityForResult(intent, 0); } } public void stop() { if (mContext.get() != null) { if (ActivityCompat.checkSelfPermission(mContext.get(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext.get(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } locationManager.removeUpdates(mGPSLocation); } } }
GPSProviderStatus类
public class GPSProviderStatus { //用户手动开启GPS public static final int GPS_ENABLED = 0; //用户手动关闭GPS public static final int GPS_DISABLED = 1; //服务已停止,并且在短时间内不会改变 public static final int GPS_OUT_OF_SERVICE = 2; //服务暂时停止,并且在短时间内会恢复 public static final int GPS_TEMPORARILY_UNAVAILABLE = 3; //服务正常有效 public static final int GPS_AVAILABLE = 4; }
然后就可以在Main里直接用了只要implements
public class MainActivity extends AppCompatActivity implements GPSLocationListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GPSLocationManager gpsLocationManager = GPSLocationManager.getInstances(MainActivity.this); gpsLocationManager.start(MainActivity.this); } @Override public void UpdateLocation(Location location) { if (location != null) { Toast.makeText(MainActivity.this, location.getLongitude() +","+location.getLatitude(), Toast.LENGTH_SHORT).show(); } } @Override public void UpdateStatus(String provider, int status, Bundle extras) { Toast.makeText(MainActivity.this, "定位类型:" + provider, Toast.LENGTH_SHORT).show(); } @Override public void UpdateGPSProviderStatus(int gpsStatus) { } }
GitHub地址 下载前给star
标签:android,void,location,import,Android,GPS,public,SDK From: https://www.cnblogs.com/Frank-dev-blog/p/16823823.html