Get current location name of user without using gps or internet but by using Network_Provider in android

前端 未结 6 626
南方客
南方客 2020-12-31 08:50

This question is directly related to the same prevailing stackoverflow question at \"Android: get current location of user without using gps or internet\" where the accepted

6条回答
  •  执笔经年
    2020-12-31 09:27

    You can try getting a country level accuracy using the Locale object or using the Telephony service. No internet or GPS required.

    Getting country code from Locale:

    String locale = context.getResources().getConfiguration().locale.getCountry();
    

    Getting country code from Android's Telephony service:

    TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    // Will work on all networks. Only provide the SIM card's country
    String countryCode = tm.getSimCountryIso();
    
    // Might not work well on CDMA networks. Will provide the country code
    // for the country the device is currently in.
    String currentCountryCode = tm.getNetworkCountryIso();
    

    Better code samples and discussion here.

提交回复
热议问题