Created
March 7, 2016 02:14
-
-
Save rahul01/693a8731e4a2ea9dc839 to your computer and use it in GitHub Desktop.
regionselection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.test.regionselection; | |
| import android.Manifest; | |
| import android.content.Context; | |
| import android.content.pm.PackageManager; | |
| import android.location.Address; | |
| import android.location.Geocoder; | |
| import android.location.Location; | |
| import android.location.LocationManager; | |
| import android.os.Bundle; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.widget.TextView; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import java.util.Locale; | |
| public class MainActivity extends AppCompatActivity { | |
| private static final int LOCATION_REQUEST = 1; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| TextView country = (TextView) findViewById(R.id.country); | |
| String[] toCheck = {Manifest.permission.ACCESS_FINE_LOCATION, | |
| Manifest.permission.ACCESS_COARSE_LOCATION}; | |
| ActivityCompat.requestPermissions(this, toCheck, LOCATION_REQUEST); | |
| String countryNameFromLocale = getRegionFromLocale(); | |
| String countryNameFromGPS = ""; | |
| try { | |
| countryNameFromGPS = getRegionFromGPS(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| if (countryNameFromLocale.equals(countryNameFromGPS)) { | |
| country.setText(countryNameFromGPS); | |
| } else { | |
| country.setText(R.string.select_country); | |
| } | |
| } | |
| private String getRegionFromLocale() { | |
| String region = getResources().getConfiguration().locale.getDisplayCountry(Locale.US); | |
| Log.d("MAIN:", "Region from locale : " + region); | |
| return region; | |
| } | |
| private String getRegionFromGPS() throws IOException { | |
| Geocoder geocoder; | |
| List<Address> addresses; | |
| geocoder = new Geocoder(this, Locale.getDefault()); | |
| Location location = null; | |
| String region = ""; | |
| LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
| if (ActivityCompat.checkSelfPermission(this, | |
| Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED | |
| && ActivityCompat.checkSelfPermission(this, | |
| Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | |
| location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
| } | |
| if (location != null) { | |
| addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); | |
| region = addresses.get(0).getCountryName(); | |
| } | |
| Log.d("MAIN:", "Region from GPS : " + region); | |
| return region; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment