前提・実現したいこと
実機で位置情報が表示されるようにしたいです。
発生している問題・エラーメッセージ
エミュレータでは位置情報が表示されるのですが、
実機で実行すると位置情報が表示されません。
該当のソースコード
MainActivity
1import androidx.appcompat.app.AppCompatActivity 2import android.os.Bundle 3import android.Manifest 4import android.location.LocationManager 5import android.content.pm.PackageManager 6import android.content.Context 7import android.content.Intent 8import android.location.Location 9import android.location.LocationListener 10import android.widget.TextView 11import android.provider.Settings 12import android.widget.Toast 13import android.util.Log 14import androidx.core.app.ActivityCompat 15import androidx.core.content.ContextCompat 16 17 18class MainActivity : AppCompatActivity(), LocationListener { 19 20 // lateinit: late initialize to avoid checking null 21 private lateinit var locationManager: LocationManager 22 23 override fun onCreate(savedInstanceState: Bundle?) { 24 super.onCreate(savedInstanceState) 25 setContentView(R.layout.activity_main) 26 27 if (ContextCompat.checkSelfPermission(this, 28 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 29 ActivityCompat.requestPermissions(this, 30 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 31 1000) 32 } else { 33 locationStart() 34 35 if (::locationManager.isInitialized) { 36 locationManager.requestLocationUpdates( 37 LocationManager.GPS_PROVIDER, 38 1000, 39 50f, 40 this) 41 } 42 43 } 44 } 45 46 private fun locationStart() { 47 Log.d("debug", "locationStart()") 48 49 // Instances of LocationManager class must be obtained using Context.getSystemService(Class) 50 locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager 51 52 val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager 53 54 if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 55 Log.d("debug", "location manager Enabled") 56 } else { 57 // to prompt setting up GPS 58 val settingsIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS) 59 startActivity(settingsIntent) 60 Log.d("debug", "not gpsEnable, startActivity") 61 } 62 63 if (ContextCompat.checkSelfPermission(this, 64 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 65 ActivityCompat.requestPermissions(this, 66 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1000) 67 68 Log.d("debug", "checkSelfPermission false") 69 return 70 } 71 72 locationManager.requestLocationUpdates( 73 LocationManager.GPS_PROVIDER, 74 1000, 75 50f, 76 this) 77 78 } 79 80 override fun onRequestPermissionsResult( 81 requestCode: Int, permissions: Array<String>, grantResults: IntArray) { 82 if (requestCode == 1000) { 83 // 使用が許可された 84 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 85 Log.d("debug", "checkSelfPermission true") 86 87 locationStart() 88 89 } else { 90 // それでも拒否された時の対応 91 val toast = Toast.makeText(this, 92 "これ以上なにもできません", Toast.LENGTH_SHORT) 93 toast.show() 94 } 95 } 96 } 97 98 override fun onStatusChanged(provider: String, status: Int, extras: Bundle) { 99 100 } 101 102 override fun onLocationChanged(location: Location) { 103 // Latitude 104 val textView1 = findViewById<TextView>(R.id.text_view1) 105 val str1 = "Latitude:" + location.getLatitude() 106 textView1.text = str1 107 108 // Longitude 109 val textView2 = findViewById<TextView>(R.id.text_view2) 110 val str2 = "Longtude:" + location.getLongitude() 111 textView2.text = str2 112 } 113 114 override fun onProviderEnabled(provider: String) { 115 116 } 117 118 override fun onProviderDisabled(provider: String) { 119 120 } 121 122}
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:padding="20dp" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/text_view1" 12 android:textSize="20sp" 13 android:layout_margin="20dp" 14 android:textColor="#44f" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 18 <TextView 19 android:id="@+id/text_view2" 20 android:textSize="20sp" 21 android:textColor="#f44" 22 android:layout_margin="20dp" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" /> 25 26</LinearLayout>
Manifest
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="jp.co.stv_tech.test_gps"> 4 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 5 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:roundIcon="@mipmap/ic_launcher_round" 12 android:supportsRtl="true" 13 android:theme="@style/AppTheme"> 14 <activity android:name=".MainActivity"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 23</manifest>
試したこと
何を試したらよいかすらわからず、調べてみたのですが、
解決に至る情報は見当たりませんでした。
実機では表示されない原因を解決するためにはどんな文言で調べていくべきでしょうか?
ちなみにkotlin GPS 実機 エミュレータ 等で検索しておりました。
実機でのログ情報を確認して、問題の切り分けをするべきではないでしょうか?
承知いたしました。
正直代々的にエラーが出た時にしかログに着目することなかったので、これからログについても勉強してみます。
端末がGPSの電波を確実に受信できると思われる屋外に持ち出しても何も表示されないのでしょうか?私の手持ちの端末(Huawei Honor 8, Android 7.0)では正常に動作するようです。
仰る通りでした。
窓際で試してみたところ、
正常に動作いたしました。
ありがとうございます。
無事解決できたのなら良かったです。できましたら、ご自身で回答欄に解決に至った内容(一言二言で結構です)を記した上でそれをベストアンサーとし、質問を解決状態にしてください。
承知いたしました。
ご丁寧にありがとうございます。
回答1件
あなたの回答
tips
プレビュー