在android中只使用wifi获取当前位置

时间:2016-01-06 10:03:55

标签: android eclipse location

当我使用GpsProvider时,我得到了当前的位置。但是,只有当我使用wifi获得wifi网络时才需要获取当前位置。我使用NetworProvider来实现它,但我不能 我使用的代码如下

    public static void requestSingleUpdate(final Context context,
        final LocationCallback callback) {

    final LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);

    boolean isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (isNetworkEnabled) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);

        locationManager.requestSingleUpdate(criteria,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        callback.onNewLocationAvailable(location);
                    }

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

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                }, null);}  

我的许可如下。

 <uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />\
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission     android:name="android.permission.ACCESS_COARSE_LOCATION"/>

谢谢:)

2 个答案:

答案 0 :(得分:2)

检查WiFi是否启用: -

Edit>Permute Lines>Unique

答案 1 :(得分:2)

<强> ConnectivityManager

  

回答有关网络连接状态的查询的类。它   还会在网络连接发生变化时通知应用程序得到一个   通过调用此类的实例   Context.getSystemService(Context.CONNECTIVITY_SERVICE)。

<强> TYPE_WIFI

  

WIFI数据连接。活动时,所有数据流量都将使用此功能   默认情况下网络类型的界面(它有默认路由)。

您可以在逻辑

下使用
<form novalidate role="form" name="filterForm" class="form-inline">
  <div ng-repeat="criteria in criterias">
    <div class="m-b">
       <div class="form-group s-b">
         <span>Country</span>
           <select class="form-control" ng-options="item for item in country" name="country" ng-model="classification.country">
           </select>
         </div>
         <div class="form-group s-b">
           <span>State</span>
             <select ng-options="item for item in state" class="form-control" name="state" ng-model="criteria.state"  style="max-width:50%">
             </select>
          </div>
          <div class="form-group s-b">
            <span>City</span>
              <select class="form-control"  ng-options="item for item in Cities"  name="account" ng-model="criteria.city" style="max-width:50%;">
              </select>
          </div>
          <div class="form-group s-b">
            <span>Predicate</span>
            <select class="form-control" name="account" ng-model="criteria.predicate">
               <option value="matches">Matches</option>
               <option value="not-matches">Not Matches</option>
             </select>
           </div>
           <div class="form-group s-b">
             <span>Value</span>
             <select class="form-control" name="account" ng-model="criteria.value"  style="min-width:100px;">
             </select>
           </div>
           <div class="form-group">
             <span>
               <button class="btn btn-sm btn-primary pad-btn" type="submit" ng-click="addCriteria()"><i class="fa fa-plus"></i>
               </button>
               <button class="btn btn-sm btn-danger pad-btn" type="submit" ng-click="deleteCriteria(criteria)"><i
                        class="fa fa-minus"></i></button>
             </span>
           </div>
         </div>
       </div>
       <button class="btn btn-sm btn-primary pad-btn align-center" type="submit" ng-click="searchParams(filterForm)">Submit
                <i class="fa fa-check"></i></button>
       <button class="btn btn-sm btn-warning pad-btn align-center" type="submit" ng-click="resetFilter()">Reset
                <i class="fa fa-reply"></i></button>
     </form>

清单文件中添加此内容。

ConnectivityManager connManagerObj = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo WifiObj = connManagerObj.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (WifiObj.isConnected()) {
    //Add your Code
}