关闭

Android的Location功能

标签: AndroidGPSLocationManager
215人阅读 评论(0) 收藏 举报
分类:

 public class  MainActivity extends Activity {  
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}
    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" />

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37537次
    • 积分:682
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:74篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论