使位置离线 [英] Getting location offline

查看:62
本文介绍了使位置离线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在离线时获取当前所在地的经度和纬度值,并将当前位置保存到其数据库中.当移动数据和wifi均已关闭但GPS处于开启状态时,是否可以获取设备的经度和纬度?

I want to get the value of Longitude and Latitude of my current locatoin when offline and save the current location to its database. Is it possible to get the longitude and latitude of the device when mobile data and wifi are off but the GPS is ON?

推荐答案

只需使用此代码.确保用户未在其位置设置中选择仅wifi或仅网络选项.它必须是高精度或仅GPS.这段代码将起作用.

Just use this code. Make sure the user hasn't selected wifi only or network only option in his location setings. it has to be high accuracy or GPS only. This piece of code will work.

public class Location extends AppCompatActivity {
LocationManager locationManager;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    mContext=this;
    locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
            2000,
            10, locationListenerGPS);
    isLocationEnabled();

}

LocationListener locationListenerGPS=new LocationListener() {
    @Override
    public void onLocationChanged(android.location.Location location) {
        double latitude=location.getLatitude();
        double longitude=location.getLongitude();
        String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
        Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};


protected void onResume(){
    super.onResume();
    isLocationEnabled();
}

private void isLocationEnabled() {

    if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Enable Location");
        alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
        alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
    else{
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Confirm Location");
        alertDialog.setMessage("Your Location is enabled, please enjoy");
        alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
}
}

requestLocationUpdates方法的参数如下:

The parameters of requestLocationUpdates methods are as follows:

provider:我们要向其注册的提供商的名称. minTime:位置更新之间的最小时间间隔(以毫秒为单位). minDistance:位置更新之间的最小距离(以米为单位). listener:一个LocationListener,其每次位置更新都会调用onLocationChanged(Location)方法.

provider: the name of the provider with which we would like to register. minTime: minimum time interval between location updates (in milliseconds). minDistance: minimum distance between location updates (in meters). listener: a LocationListener whose onLocationChanged(Location) method will be called for each location update.

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

为低于lollipop的版本为清单文件添加上述权限,为棉花糖和更高版本的清单文件使用运行时权限.

Add above permissions to manifest file for the version lower than lollipop and for marshmallow and higher version use runtime permission.

这篇关于使位置离线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆