package com.example.nemophila;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import android.Manifest;
import android.os.Bundle;
public class GpsActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
private final ActivityResultLauncher<String>
requestPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
isGranted -> {
if (isGranted) {
locationStart();
}
else {
Toast toast = Toast.makeText(this,
"これ以上なにもできません", Toast.LENGTH_SHORT);
toast.show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION);
}
else{
locationStart();
}
}
private void locationStart(){
Log.d("debug","locationStart()");
// LocationManager インスタンス生成
locationManager =
(LocationManager) getSystemService(LOCATION_SERVICE);
if (locationManager != null && locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER)) {
Log.d("debug", "location manager Enabled");
} else {
// GPSを設定するように促す
Intent settingsIntent =
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
Log.d("debug", "not gpsEnable, startActivity");
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
Log.d("debug", "checkSelfPermission false");
return;
}
//解決しているはずの問題にエラーが起きています
//実行はできます
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 50, this);
}
@SuppressLint("QueryPermissionsNeeded")
private void moveToGMap(Location location){
String str1 = String.valueOf(location.getLatitude());
String str2 = String.valueOf(location.getLongitude());
Uri uri = Uri.parse("geo:"+str1+","+str2+"?z=16");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
@Override
public void onLocationChanged(Location location) {
// 緯度の表示
TextView textView1 = findViewById(R.id.text_view1);
String str1 = "Latitude:"+location.getLatitude();
textView1.setText(str1);
// 経度の表示
TextView textView2 = findViewById(R.id.text_view2);
String str2 = "Longitude:"+location.getLongitude();
textView2.setText(str2);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}