Android > À§Ä¡Á¤º¸ - À§Ä¡ ¼³Á¤ ¾×ƼºñƼ·Î À̵¿½ÃÄÑÁÖ±â
µî·ÏÀÏ : 2017-07-06 17:56
Á¶È¸¼ö : 53,164
À̹ø Æ÷½ºÆ®¿¡¼´Â À§Ä¡ ¼³Á¤ ¾×ƼºñƼ·Î À̵¿½ÃÄÑÁÖ´Â ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
1. ¸ÕÀú À§Ä¡ Á¤º¸¸¦ ±¸Çϱâ À§ÇÑ ±ÇÇÑÀ» ¼³Á¤ÇÕ´Ï´Ù.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" > </uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission>
2. LocationListener ÀÎÅÍÆäÀ̽º onProviderDisabled ÇÔ¼ö¿¡¼ 󸮵Ǵ ¹æ¹ýÀ» È®ÀΠüũÇØ º¸½Ã±â ¹Ù¶ø´Ï´Ù.
MainActivity.class
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity {
Geocoder geoCoder;
LocationManager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ÁÖ¼Ò¸¦ È®ÀÎÇϱâ À§ÇÑ Geocoder KOREA ¿Í KOREAN µÑ´Ù °¡´É
geoCoder = new Geocoder(this, Locale.KOREAN);
// startLocationService
startLocationService();
}
private void startLocationService() {
// get manager instance
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// set listener
GPSListener gpsListener = new GPSListener();
long minTime = 10000;
float minDistance = 0;
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,minTime, minDistance, gpsListener);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
//Toast.makeText(getApplicationContext(),
// "Location Service started.\nyou can test using DDMS.", 2000)
// .show();
}
private class GPSListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// capture location data sent by current provider
Toast.makeText(MainActivity.this, "onLocationChanged", Toast.LENGTH_SHORT).show();
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
String msg = "Latitude : " + latitude + "\nLongitude:" + longitude;
Log.i("GPSLocationService", msg);
Toast.makeText(getApplicationContext(), msg, 2000).show();
StringBuffer juso = new StringBuffer();
try {
// À§µµ,°æµµ¸¦ ÀÌ¿ëÇÏ¿© ÇöÀç À§Ä¡ÀÇ ÁÖ¼Ò¸¦ °¡Á®¿Â´Ù.
List<Address> addresses;
addresses = geoCoder.getFromLocation(latitude, longitude, 1);
for (Address addr : addresses) {
int index = addr.getMaxAddressLineIndex();
for (int i = 0; i <= index; i++) {
juso.append(addr.getAddressLine(i));
juso.append(" ");
}
juso.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), String.valueOf(juso), 2000)
.show();
manager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "onProviderDisabled", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(MainActivity.this)
.setTitle("°æ°í")
.setMessage("GPS°¡ ²¨Á®ÀÖ½À´Ï´Ù.\n ¡®À§Ä¡ ¼ºñ½º¡¯¿¡¼ ¡®Google À§Ä¡ ¼ºñ½º¡¯¸¦ üũÇØÁÖ¼¼¿ä")
.setCancelable(false) // Back Button µ¿ÀÛ ¾ÈÇϵµ·Ï ¼³Á¤
// "°è¼Ó" ¹öÆ°
.setPositiveButton("¼³Á¤",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Toast.makeText(MainActivity.this, "°è¼Ó Çϼ¼¿ä~", Toast.LENGTH_SHORT).show();
}
})
// "Á¾·á" ¹öÆ°
.setNegativeButton("Á¾·á", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Á¾·á ¹öÆ° ´·¶³×¿ä", Toast.LENGTH_SHORT).show();
}
})
.show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this, "onProviderEnabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "onStatusChanged", Toast.LENGTH_SHORT).show();
}
}
}
3. À§Ä¡Á¤º¸»óÅ°¡ ÄÑÁ® ÀÖ´ÂÁö¸¦ È®ÀÎÇÏ´Â ¹æ¹ýÀº ¾Æ·¡¿Í °°½À´Ï´Ù.
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//À§Ä¡Á¤º¸ ´ÙÀ̾ó·Î±×¸¦ ¶ç¿î´Ù.
}