简体   繁体   中英

NETWORK_PROVIDER Not Found in Android ICS 4.1

The Title Say all , im using this :

 public int onStartCommand(Intent get,int num1,int num2){

 if (lm != null){

      lm.removeUpdates(this);

  }

 prefs = getSharedPreferences("Prefs", 
         Context.MODE_PRIVATE);





lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_COARSE );
String provider = lm.getBestProvider( criteria, false );
////* Just GPS is Listed in this List , Google Location is Enabled but Not Detected       
/*and Note the Parameter 'false' to get the list of all avaible provider,enable or not 
Toast.makeText(this, provider, Toast.LENGTH_LONG).show(); */
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);




 if(IsFromWidget != true){
str2 = get.getStringExtra("num");   
 }
CheckNetworkState();
return 0;


}

  public void onDestroy() 
 {
 super.onDestroy();
 if (lm != null){

      lm.removeUpdates(this);

  }
 }
private void CheckNetworkState() {


                 if (lm != null){

                      lm.removeUpdates(this);

                  }
                 stopSelf();
                 }






    }





public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

lat = arg0.getLatitude();
lon = arg0.getLongitude();


if (lat == 0 && lon == 0){


}

}


}

if(IsFromWidget = true){



if (lat == 0 && lon == 0){


}
}


stopSelf();
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub


      } 



public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}



@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

 }

but its return only GPS and my Google Location Settings are Enabled , and im Connected 3G or Wifi ,its worked fine when i was using Android 2.3.x , please does not pay attention at Code Errors , i Edited the Code before Posting Here .

Thanks .

Remember to set your permissions in AndroidManifest.xml

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

If you are trying to get your location you can do this:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Criteria crit = new Criteria();
provider = lm.getBestProvider(crit, false);
Location location = lm.getLastKnownLocation(provider);

And set up a LocationListener class

public class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location loc){


    }

    public void onProviderDisabled(String provider) {


    }


    public void onProviderEnabled(String provider) {

    }


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

    }
}

That's a good start for you at least. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM