티스토리 뷰
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>
메니페스트에 이거 넣어야 한다.
'프로그래밍 > 안드로이드' 카테고리의 다른 글
[안드로이드] 간단한 음악 플레이어 (0) | 2018.10.15 |
---|---|
[안드로이드] 구글 지도를 띄우고 마커도 띄우기 (0) | 2018.10.14 |
[안드로이드] Url에 요청해서 Json 받아오기2 (JSON 요청과 outputstream 요청) (0) | 2018.10.11 |
[안드로이드] Url에 요청해서 Json 받아오기 (Get) (0) | 2018.10.10 |
[안드로이드 메모] 버튼 받는 방법 (0) | 2018.08.13 |