package com.example.gps0719;
import java.io.IOException;
import java.util.List;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
LocationManager locationManager;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkMyLocation();
getAddress();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void checkMyLocation(){
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 10000, 100, new MyLocationListener());
if(provider == null){ //gps off이면 network통해서 받아오도록..
Toast.makeText(getBaseContext(), "no GPS Provider", Toast.LENGTH_SHORT).show();
provider = LocationManager.NETWORK_PROVIDER;
location = locationManager.getLastKnownLocation(provider);
}
location = locationManager.getLastKnownLocation(provider);
if(location == null){
try{
Thread.sleep(10000);
}catch(InterruptedException e){
e.printStackTrace();
}
location = locationManager.getLastKnownLocation(provider);
}
}
private class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public void getAddress() {
Geocoder geoCoder = new Geocoder(this);
double lat = location.getLatitude();
double lng = location.getLongitude();
List<Address> addresses = null;
try {
addresses = geoCoder.getFromLocation(lat, lng, 5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(addresses.size()>0){
Address mAddress = addresses.get(0);
//String Area = mAddress.getCountryName();
String mAddressStr = "Phone is near this address - "+mAddress.getCountryName()+" "
+mAddress.getPostalCode()+" "
+mAddress.getLocality()+" "
+mAddress.getThoroughfare()+" "
+mAddress.getFeatureName();
Toast.makeText(getBaseContext(), mAddressStr, Toast.LENGTH_SHORT).show();
}
}
}
대체 기기마다 왜 틀린건지 모르겠어요.
갤1에서는 위소스를 실행시키면 주소값 잘 받아옵니다.
근데 갤2에서 똑같이 설치해서 실행해보면 앱이 죽어요;;
전에도 다른 소스로 gps 실행을 해보면
갤2에서는 주소값 잘 리턴하던 소스가
갤3에서는 또 먹통이고;;
이거 어찌해야 하나요?
진짜 제대로 된 gps예제 소스 링크 좀 걸어주시면 감사할것 같아요