質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

1回答

3704閲覧

android studio位置情報の取得

Horin

総合スコア9

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/12/19 10:32

編集2016/12/21 05:48

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カードなどに値を表示したいです。 ```java 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); }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yona

2016/12/19 11:48

コードはコード用の記述に修正してください。また、エラーログも質問に追記してください。
guest

回答1

0

GPS情報取得が一向に終わらないので

最初にemulatorでテストです
emulatorで緯度経度を設定してアプリに送信できますので
コードが正しく動作しているかわかります。

java

1ActivityCompat.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

どこぞのページをまるまるコピペされたのでしょうけれど
古い情報だとそのままではうまくいきません
半年前でも十分古い場合がありますので

投稿2016/12/20 06:33

編集2016/12/20 06:36
aja

総合スコア3733

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Horin

2016/12/21 04:49

permissionのチェックはandroid4.0でも必要なのですか?
aja

2016/12/21 05:22

android4.0ではruntime permissionはありません、インストール時のみです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問