Android的Location功能

原创 2015年07月08日 10:35:38

 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" />

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android_Location_V1.3.0.jar包

  • 2015-12-23 11:36
  • 179KB
  • 下载

[x-Code7新功能之四]Core Location和MapKit框架练习

[x-Code7新功能之四]Core Location和MapKit框架练习

ArcGIS For Android Location Demo

  • 2015-06-02 17:09
  • 115KB
  • 下载

IOS Core Location 定位功能

Core Location可以利用三种技术来实现该功能: 蜂窝基站三角网定位根据手机所属范围内的手机基站的位置进行计算来确定当前位置。蜂窝基站三角网定位在城市和其他手机基站密度较高的区域非常准确,而...

一个简单的例子看明白如何利用window.location.hash实现ajax操作时浏览器的前进/后退功能

我们知道JavaScript中很早就提供了window.history对象,利用history对象的forward()、go()、back()方法能够方便实现不同页面之间的前进、后退等这种导航功能。但...

android location包

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)