此代碼為Location功能的簡單封裝。
package arui.csdn.net;
- import static Android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
-
- public class LocationUtil {
-
- public static boolean isEnabled(Context context) {
- LocationManager mLocationManager = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
- return true;
- }
- if (mLocationManager
- .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
- return true;
- }
- return false;
- }
-
- public static void startLocationService(Context context) {
- LocationManager mLocationManager = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
- 1000L, 10, mLocationListener);
- mLocationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER, 1000L, 10,
- mLocationListener);
- }
-
- public static void stopLocationService(Context context) {
- LocationManager mLocationManager = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- mLocationManager.removeUpdates(mLocationListener);
- }
-
- public static Location getLocationByGPS(Context context) {
- LocationManager mLocationManager = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- Location location = mLocationManager
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- return location;
- }
-
- public static Location getLocationByNetwork(Context context) {
- LocationManager mLocationManager = (LocationManager) context
- .getSystemService(Context.LOCATION_SERVICE);
- Location location = mLocationManager
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- return location;
- }
-
- public static void openLocationSetting(Activity act, int requestCode) {
- act.startActivityForResult(new Intent(ACTION_LOCATION_SOURCE_SETTINGS),
- requestCode);
- }
-
- private static final LocationListener mLocationListener = new LocationListener() {
-
- public void onLocationChanged(Location location) {
- // 當坐標改變時觸發此函數,如果Provider傳進相同的坐標,它就不會被觸發
- System.out.println("onLocationChanged:" + location.getLatitude());
- }
-
- public void onProviderDisabled(String provider) {
- // Provider被disable時觸發此函數,比如GPS被關閉
- System.out.println("onProviderDisabled:" + provider);
- }
-
- public void onProviderEnabled(String provider) {
- // Provider被enable時觸發此函數,比如GPS被打開
- System.out.println("onProviderEnabled:" + provider);
- }
-
- public void onStatusChanged(String provider, int status, Bundle extras) {
- // Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數
- System.out.println("onStatusChanged:" + status);
- }
- };
- }
需要增加權限:
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />