@Nullable
@Override
public
View onCreateView(LayoutInflater inflater,
@Nullable
ViewGroup container,
@Nullable
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment3, container,
false
);
bt2 = rootView.findViewById(R.id.button2);
textView = rootView.findViewById(R.id.textView);
textView.setMovementMethod(
new
ArrowKeyMovementMethod());
bt2.setOnClickListener(
new
View.OnClickListener() {
@RequiresApi
(api = Build.VERSION_CODES.R)
@Override
public
void
onClick(View v) {
if
(ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
;
}
LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GPSListener gpsListener =
new
GPSListener(manager);
long
minTime =
10000
;
float
minDistance =
0
;
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, gpsListener);
}
});
return
rootView;
}
private
class
GPSListener
implements
LocationListener {
LocationManager manager;
public
GPSListener(LocationManager manager) {
this
.manager = manager;
}
@RequiresApi
(api = Build.VERSION_CODES.N)
@Override
public
void
onLocationChanged(Location location) {
String message =
"경도 : "
+ location.getLongitude() +
"\n위도 : "
+ location.getLatitude() +
"\n정확도 : "
+ location.getAccuracy() +
"\n위성수"
+ usedSatellites +
"\n"
;
textView.append(message);
String now =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
).format(
new
Date());
String contents =
"Log 생성 : "
+now+
"\n"
+ message;
String inputData = contents;
try
{
FileOutputStream fos = getContext().openFileOutput(
"data.txt"
, Context.MODE_APPEND);
PrintWriter writer =
new
PrintWriter(fos);
writer.println(inputData);
writer.close();
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
}
};
}
public
void
onProviderDisabled(String provider) {
}
public
void
onProviderEnabled(String provider) {
}
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {
}
}