0

My application uses MapView from Google API 2.0. Mobile data usage is a concern, so I want to limit Google Maps to use Wifi only. When I block all network connections, I see cached map data which is all I need for most cases.

But I also have some background tasks that I want running even on mobile data.

So what I have to accomplish is using Google Maps with Wifi only mode without putting any network restrictions to the rest of the application (and to other apps, of course).

I guess I could make it work if I could just put MapView to "offline" mode, preventing all network usage by it. I could just switch it on and off depending on wifi availability.

  • at the map screen (onCreate() maybe) check network connectivity type http://stackoverflow.com/questions/2802472/detect-network-connection-type-on-android if not WIFI don't load the map, show some toast ... etc do whatever you want – Yazan Mar 13 '16 at 12:47
  • That's plan B actually. But I am able to show the map with cached data when there is no network available. I just want to do that when there is mobile data available. – ertanyavuz Mar 13 '16 at 12:51
  • i don't know what you want!!! – Yazan Mar 13 '16 at 12:55

1 Answers1

0

Add following class to ur code.

public class NetworkState {

    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;


    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        }
        return TYPE_NOT_CONNECTED;
    }
}

inside MapClass.java

if(NetworkState.getConnectivityStatus(context) != 2)
    mapMethod();
vabhi vab
  • 419
  • 4
  • 11