Android studioで位置情報を表示したいのですが,GPS情報取得が一向に終わらないので,ネットワーク経由からやろうとしています。しかし,Unfortunately, GPSApp has stoppedと出て,アプリが落ちてしまいます。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
このふたつをmanifestに記載するとGPSの取得待ちで表示されず,ネットワーク経由のみだと上記のようになります。
中国製の端末でバージョンがandroid4.0.3です。
android studioは2.2.2を使用しています。
最終的には加速度と一緒にsdカードなどに値を表示したいです。
package com.example.g1323073.gps05;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private LocationManager mLocationManager = null;
private LocationListener mLocationListener = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
// ToDo: GPS 有効にしてくれー
}
mLocationListener = new LocationListener() {
// http://developer.android.com/reference/android/location/Location.html
public void onLocationChanged(Location location) {
String out =
"---------- Location --------\n" +
"緯度(Latitude)\n\t" + String.valueOf(location.getLatitude()) + " 度\n" +
"経度(Longitude)\n\t" + String.valueOf(location.getLongitude()) + " 度\n" +
"高度(Altitude)\n\t" + String.valueOf(location.getAltitude()) + " m\n" +
"精度(Accuracy)\n\t" + String.valueOf(location.getAccuracy()) + " m\n" +
"速度(Speed)\n\t" + String.valueOf(location.getSpeed()) + " m/s\n" +
"方角(Bearing)\n\t" + String.valueOf(location.getBearing()) + " 度\n" +
"時刻(Time)\n\t" + String.valueOf(location.getTime()) + " ミリ秒\n" +
"\n";
Bundle extra = location.getExtras();
if (extra != null) {
int satellites = extra.getInt("satellites");
out += "extra.satellites\n\t" + String.valueOf(satellites);
}
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(out);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
}
protected void onStart() { // ⇔ onStop
super.onStart();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000 * 1/*mSec*/, 1/*meter*/,
mLocationListener);
}
protected void onStop() { // ⇔ onStart
super.onStop();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLocationManager.removeUpdates(mLocationListener);
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
0
GPS情報取得が一向に終わらないので
最初にemulatorでテストです
emulatorで緯度経度を設定してアプリに送信できますので
コードが正しく動作しているかわかります。
ActivityCompat.checkSelfPermission...
permissionのチャックが行われていないので動作していません
ユーザーに許可を求めるダイアログが出ないといけないので
以前にコメントしたのですが...
https://teratail.com/questions/57026
https://akira-watson.com/android/gps.html
このふたつをmanifestに記載するとGPSの取得待ちで表示されず,ネットワーク経由のみだと上記のようになります。
あなたのコードではACCESS_FINE_LOCATIONしか見ていないので
ACCESS_COARSE_LOCATIONもpermission checkが必要です
もっともLocationManager.GPS_PROVIDERを設定しているので
このままではネットワークでの測位はやれません
GPS,ネットワークも含めた測位を考えているならば
FusedLocationProviderApiを使ったほうがいいです
リンク内容
それから、「"」これは微妙に違うのでコピペするときは気をつけないといけませんね
”android.permission.ACCESS_COARSE_LOCATION” // X
"android.permission.ACCESS_COARSE_LOCATION" // O
どこぞのページをまるまるコピペされたのでしょうけれど
古い情報だとそのままではうまくいきません
半年前でも十分古い場合がありますので
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
yona
2016/12/19 20:48
コードはコード用の記述に修正してください。また、エラーログも質問に追記してください。