Java ์๋๋ก์ด๋ ์น๋ทฐ์์ ์์น์๋น์ค๊ฐ ๊บผ์ ธ์๋์ง ํ์ธํ๊ณ ๊บผ์ ธ์์ผ๋ฉด ์์น ์๋น์ค๋ฅผ Activeํ๋ ์ฐฝ์ด ๋จ๋๋ก ํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค.
private void checkLocationEnabled() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = false;
boolean networkEnabled = false;
try {
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
if (!gpsEnabled && !networkEnabled) {
// ์์น ์๋น์ค๊ฐ ๊บผ์ ธ์๋ ๊ฒฝ์ฐ
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("์์น ์๋น์ค ํ์ฑํ");
builder.setMessage("์ด ์ฑ์ ์ฌ์ฉํ๋ ค๋ฉด ์์น ์๋น์ค๊ฐ ํ์ํฉ๋๋ค. ์์น ์๋น์ค๋ฅผ ํ์ฑํ ํ์๊ฒ ์ต๋๊น?");
builder.setPositiveButton("ํ์ฑํ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton("์ทจ์", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
์์ ์ฝ๋๋ฅผ MainActivity.java์ ์๋ public class MainActivity extends AppCompatActivity ์๋์ ์์ฑํ์๊ณ
์์น ์๋น์ค๋ฅผ ํ์ฑํํ๊ธฐ ์ ๋นํ ์์น์์
checkLocationEnabled();
์๋์์ผ์ฃผ๋ฉด ๋ฉ๋๋ค.