현재 위치의 주소를 읽는 소스입니다..
이걸 처음에는 액티비티 안에 구현을 했는데....
따로 gps.클래스로 만들어서....
반환되는 주소값만 다른 액티비티 등에서 읽어서 사용하고 싶은데요....
방법을 모르겠어요...
다른 액티비티에서 gps.class 자체 호출하는 방법도 모르겠습니다.
힌트 좀 주시면 감사하겠습니다.^^
public class Location_gps extends Activity implements LocationListener
,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Context mContext;
private GoogleApiClient mGoogleApiClient;
private static final long INTERVAL = 1000 *2;
private static final long FASTEST_INTERVAL = 1000 *5;
private LocationRequest mLocationRequest;
Location mCurrentLocation;
private static final int REQUEST_CODE_LOCATION = 2;
public String place;
public String detailjuso;
GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
chkGpsService();
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
//.enableAutoManage(Location_gps.this, 0, this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
updateUI();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void updateUI() {
Log.d("NOTICE", "UI update initiated .............");
if (null != mCurrentLocation) {
//Toast.makeText(MainActivity.this, "~~~~~~~~~", Toast.LENGTH_SHORT).show();
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
Toast.LENGTH_SHORT).show();
new GetAddressTask().execute(mCurrentLocation);
} else {
//Toast.makeText(MainActivity.this, "location null", Toast.LENGTH_SHORT).show();
Log.d("NOTICE", "location is null ...............");
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
public class GetAddressTask extends AsyncTask<Location, Void, String>
{
@Override
protected String doInBackground(Location... params) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(Location_gps.this);
Location location = params[0];
//String addrStr = null;
List<Address> addresses = null;
try {
addresses = (List<Address>)geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
Address addr = addresses.get(0);
detailjuso = addr.getLocality()+ " "+ addr.getThoroughfare();
//detailjuso = addr.getAddressLine(0);
//addrStr = addr.getAddressLine(0) + ", "+addr.getAdminArea() + ", "+addr.getCountryCode();
} catch (IOException e) {
e.printStackTrace();
Log.e("KSJ", "Service Not Available..");
}
if (addresses != null && addresses.size() > 0) {
return detailjuso;
}
return null;
}
protected void onPostExecute(String result) {
place = result;
//t1.t1where.setText(place);
Toast.makeText(Location_gps.this, "Current Location : " + place, Toast.LENGTH_SHORT).show();
//juso.setText(result);
//t1where.setText(detailjuso);
}
}
protected void startLocationUpdates() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
}
} else {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CODE_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// success!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
} else {
// Permission was denied or request was cancelled
}
}
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Log.d("NOTICE", "Location update stopped .......................");
Toast.makeText(Location_gps.this, "Current Location STOP...!! ", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.e("KSJ", "mGoogleApiClient.connect() 연결되었습니다..^^");
}
@Override
public void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
Log.e("KSJ", "mGoogleApiClient.connect() 해제되었습니다..^^");
}
}