4

如何检查是否LocationManager.NETWORK_PROVIDER可用?

我启用了,AndroidManifest.xml但我需要在代码中检查它是否不可用GPS_PROVIDER。有人能帮我吗 ?

4

3 回答 3

30

如何检查 LocationManager.NETWORK_PROVIDER 是否可用?

使用此部分检查网络提供商是否已启用:

network_enabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

这将返回一个布尔值:

true - 如果启用。

false - 未启用。

另外用于检查 GPS Provider 是否已启用:

gps_enabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

这将返回一个布尔值:

true - 如果启用。

false - 未启用。

如果您希望用户启用网络或 GPS 提供商,请使用此代码将用户重定向到设置页面:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
于 2011-10-06T11:03:51.803 回答
3

因为这里总是有点奇怪;=)

我编写了一些自己的代码来检查网络提供商的可用性

把它放在你的onCreate() 方法中:

checkIfNetworkLocationAvailable(mContext_);

在你的代码中的其他地方;=)

/**
 * checks if NETWORK LOCATION PROVIDER is available
 */
private void checkIfNetworkLocationAvailable(Context context) {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean networkLocationEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(!networkLocationEnabled){
        //show dialog to allow user to enable location settings
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(mContext_.getString(R.string.txtTip));
        dialog.setMessage(R.string.msgLocationServicesDisabled);

        dialog.setPositiveButton(mContext_.getString(R.string.txtYes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
            }
        });

        dialog.setNegativeButton(mContext_.getString(R.string.txtNo), new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //nothing to do
            }
        });

        dialog.show();
    }

}

玩得开心,希望这对某人有帮助 :) 正在寻找那段代码 :=)

干杯:)

于 2012-02-17T08:41:43.023 回答
3

这是使用网络提供商和/或 GPS 提供商获取位置的代码

private Location getLocation() {            
        Location gpslocation = null;
        Location networkLocation = null;

        if(locMan==null){
          locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
        }
        try {
            if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
                gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            }
            if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
                networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
            }
        } catch (IllegalArgumentException e) {
            //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString());
            Log.e("error", e.toString());
        }
        if(gpslocation==null && networkLocation==null)
            return null;

        if(gpslocation!=null && networkLocation!=null){
            if(gpslocation.getTime() < networkLocation.getTime()){
                gpslocation = null;
                return networkLocation;
            }else{
                networkLocation = null;
                return gpslocation;
            }
        }
        if (gpslocation == null) {
            return networkLocation;
        }
        if (networkLocation == null) {
            return gpslocation;
        }
        return null;
    }

在这里它将检查 GPS 提供程序是否启用,然后将位置获取到 gpsLocation 如果还使用网络提供程序获取位置,然后检查位置对象返回的最快时间

如果启用其中一个提供程序,则将返回该位置对象

于 2011-10-06T10:41:57.330 回答