Kabir Kabir - 6 months ago 24
Android Question

Caused by: java.lang.IllegalArgumentException: provider=network

trying to find the current location of my device.code is here.

package com.example.location;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;


public class LocationActivity extends Activity
{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{

@Override
public void onLocationChanged(Location loc)
{

loc.getLatitude();
loc.getLongitude();

String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();

Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}

@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}

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

}
}
}


and logcat is

07-15 12:42:49.294: E/AndroidRuntime(3322): FATAL EXCEPTION: main
07-15 12:42:49.294: E/AndroidRuntime(3322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location/com.example.location.LocationActivity}: java.lang.IllegalArgumentException: provider=network
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.os.Looper.loop(Looper.java:137)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-15 12:42:49.294: E/AndroidRuntime(3322): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 12:42:49.294: E/AndroidRuntime(3322): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 12:42:49.294: E/AndroidRuntime(3322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-15 12:42:49.294: E/AndroidRuntime(3322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-15 12:42:49.294: E/AndroidRuntime(3322): at dalvik.system.NativeStart.main(Native Method)
07-15 12:42:49.294: E/AndroidRuntime(3322): Caused by: java.lang.IllegalArgumentException: provider=network
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.os.Parcel.readException(Parcel.java:1331)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.os.Parcel.readException(Parcel.java:1281)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:589)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:453)
07-15 12:42:49.294: E/AndroidRuntime(3322): at com.example.location.LocationActivity.onCreate(LocationActivity.java:26)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.Activity.performCreate(Activity.java:4465)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-15 12:42:49.294: E/AndroidRuntime(3322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-15 12:42:49.294: E/AndroidRuntime(3322): ... 11 more

Answer

Have you tried the same code on Real Device. I think it should work on device, Network provider causes problem on Emulator.

Edit:- Based on @Felipe comment, Many some devices may not support network provider. Use below method to check if provider is actually enabled or not.

/*Pass context as well as Provider name, you can get provider name via LocationManager class*/
     public boolean isProviderEnabled(Context ctx, String provider){
        LocationManager manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        return manager.isProviderEnabled(provider);
    }