package com.example.acid.samplelocation;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView textView;
Button button;
Button button2;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView1);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startLocationService();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopLocation();
Toast.makeText(getApplicationContext(), "GPS OFF", Toast.LENGTH_LONG).show();
}
});
}

private LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
Double latitude = location.getLatitude();
Double longitude = location.getLongitude();
textView.setText(latitude + " "+longitude);
Toast.makeText(getApplicationContext(), "onLocationChanged", Toast.LENGTH_LONG).show();

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};

LocationManager locationManager = null;

public void startLocationService() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //LOCATION_SERVICE
Location location = null;
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ //네트워크가 활성화 되어 있다면
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}else{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //마지막에 알려진 위치
}

String getLongitude = "LAST getLongitude : " + location.getLongitude();
String getLatitude = " LAST getLatitude : " + location.getLatitude();
Log.i("Location ", getLongitude + "" + getLatitude);

textView.setText(getLongitude + "" + getLatitude);
Toast.makeText(getApplicationContext(), "GPS", Toast.LENGTH_LONG).show();


long minTime = 10000; // 10초
long minDistance = 0;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
}

public void stopLocation(){
locationManager.removeUpdates(locationListener);
}


}

GPS 받고 끊고 


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

메니페스트에 이거 넣어야 한다. 

블로그 이미지

LunaStratos

Stratosphere Stream

댓글을 달아 주세요