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

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

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

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

Android Studio

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

Q&A

解決済

2回答

8539閲覧

Android Studio、位置情報取得について

matchdas3333

総合スコア12

Java

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

Android Studio

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

0グッド

0クリップ

投稿2016/06/23 02:32

Android Studioで位置情報を取得するアプリを作成しています。ロケーションマネージャーの登録をしたいのですが、エラー文が出てしまいます。

java

1package package.locationex; 2 3import android.Manifest; 4import android.content.Context; 5import android.content.pm.PackageManager; 6import android.graphics.Color; 7import android.location.Location; 8import android.location.LocationListener; 9import android.location.LocationManager; 10import android.net.Uri; 11import android.os.Bundle; 12import android.support.v4.app.ActivityCompat; 13import android.support.v7.app.AppCompatActivity; 14import android.view.Window; 15import android.widget.LinearLayout; 16import android.widget.TextView; 17 18import com.google.android.gms.appindexing.Action; 19import com.google.android.gms.appindexing.AppIndex; 20import com.google.android.gms.common.api.GoogleApiClient; 21 22public class LocationEx extends AppCompatActivity implements LocationListener { 23 24 private final static String BR = System.getProperty("line.separator"); 25 private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT; 26 private TextView tv; 27 private LocationManager manager; 28 /** 29 * ATTENTION: This was auto-generated to implement the App Indexing API. 30 * See https://g.co/AppIndexing/AndroidStudio for more information. 31 */ 32 private GoogleApiClient client; 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 //setContentView(R.layout.activity_location_ex); 38 39 requestWindowFeature(Window.FEATURE_NO_TITLE); 40 41 //レイアウトの作成 42 LinearLayout layout = new LinearLayout(this); 43 layout.setBackgroundColor(Color.WHITE); 44 layout.setOrientation(LinearLayout.VERTICAL); 45 setContentView(layout); 46 47 //テキストビューの生成 48 tv = new TextView(this); 49 tv.setText("LocationEx"); 50 tv.setTextSize(24); 51 tv.setTextColor(Color.BLACK); 52 tv.setLayoutParams(new LinearLayout.LayoutParams(WC, WC)); 53 layout.addView(tv); 54 55 //ロケーションマネージャーの取得 56 manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 57 58 // ATTENTION: This was auto-generated to implement the App Indexing API. 59 // See https://g.co/AppIndexing/AndroidStudio for more information. 60 client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 61 } 62 63 @Override 64 public void onResume() { 65 super.onResume(); 66 67 //ロケーションマネージャーのリスナー登録 68 manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 69 } 70 71 @Override 72 public void onPause() { 73 super.onPause(); 74 75 //ロケーションマネージャーの設定 76 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 77 // TODO: Consider calling 78 // ActivityCompat#requestPermissions 79 // here to request the missing permissions, and then overriding 80 // public void onRequestPermissionsResult(int requestCode, String[] permissions, 81 // int[] grantResults) 82 // to handle the case where the user grants the permission. See the documentation 83 // for ActivityCompat#requestPermissions for more details. 84 return; 85 } 86 manager.removeUpdates(this); 87 } 88 89 @Override 90 public void onLocationChanged(Location location) { 91 tv.setText("LocationEx>"+BR+"緯度:"+location.getLatitude()+BR+"軽度:"+location.getLongitude()); 92 } 93 94 @Override 95 public void onStatusChanged(String provider, int status, Bundle extras) { 96 97 } 98 99 @Override 100 public void onProviderEnabled(String provider) { 101 102 } 103 104 @Override 105 public void onProviderDisabled(String provider) { 106 107 } 108 109 @Override 110 public void onStart() { 111 super.onStart(); 112 113 // ATTENTION: This was auto-generated to implement the App Indexing API. 114 // See https://g.co/AppIndexing/AndroidStudio for more information. 115 client.connect(); 116 Action viewAction = Action.newAction( 117 Action.TYPE_VIEW, // TODO: choose an action type. 118 "LocationEx Page", // TODO: Define a title for the content shown. 119 // TODO: If you have web page content that matches this app activity's content, 120 // make sure this auto-generated web page URL is correct. 121 // Otherwise, set the URL to null. 122 Uri.parse("http://host/path"), 123 // TODO: Make sure this auto-generated app URL is correct. 124 Uri.parse("android-app://jp.ac.gifu_u.t3033180.locationex/http/host/path") 125 ); 126 AppIndex.AppIndexApi.start(client, viewAction); 127 } 128 129 @Override 130 public void onStop() { 131 super.onStop(); 132 133 // ATTENTION: This was auto-generated to implement the App Indexing API. 134 // See https://g.co/AppIndexing/AndroidStudio for more information. 135 Action viewAction = Action.newAction( 136 Action.TYPE_VIEW, // TODO: choose an action type. 137 "LocationEx Page", // TODO: Define a title for the content shown. 138 // TODO: If you have web page content that matches this app activity's content, 139 // make sure this auto-generated web page URL is correct. 140 // Otherwise, set the URL to null. 141 Uri.parse("http://host/path"), 142 // TODO: Make sure this auto-generated app URL is correct. 143 Uri.parse("android-app://jp.ac.gifu_u.t3033180.locationex/http/host/path") 144 ); 145 AppIndex.AppIndexApi.end(client, viewAction); 146 client.disconnect(); 147 } 148} 149

エラー文はこちらです。
Call requires permission which may be rejected by user:code should explicitly check to see if permission is available(with 'checkPermission')or explicitly handle a potential'SecurityException'

This inspection looks at Android API calls that have been annotated with various support annotations(such as RequiresParmission or UiThread)and flags any calls that are not using the API correctly as specified by the annotations.

どうか、よろしくお願いします。

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

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

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

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

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

guest

回答2

0

ベストアンサー

単純にエラーを消すだけであれば
targetSdkVersion 22
のようにAPIを23から下げるか

manager.requestLocationUpdates
のエラーが出ているところで、修正候補
if (ActivityCompat.checkSelfPermission
...
}
を出せば消えることは消えます

ただ、これは根本的な解決策ではありません
自分のAPI18で動かすだけなら問題ありませんが
Android6の実機ではパーミッションをインストールした後で選択できるので
位置情報取得を不許可にすると落ちます

checkSelfPermissionを使って不許可かどうかチェックして
不許可の場合は理由をユーザーに知らせて許可してもらうような実装が必要です
それでも拒否されれば何もできませんが

参考

投稿2016/06/23 10:39

aja

総合スコア3733

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

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

matchdas3333

2016/06/29 00:25

ありがとうございました。
guest

0

パーミッションの設定はしましたか?
また、Android6.xではアプリケーション内でパーミッションを許可してもらう必要があります。

投稿2016/06/23 02:35

yona

総合スコア18155

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

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

matchdas3333

2016/06/23 03:06

迅速な回答ありがとうございます。 マニフェストで <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> という記述をしているのですが、これではだめなのでしょうか?
yona

2016/06/23 03:51

バージョン次第です。 バージョンを追記してください。
matchdas3333

2016/06/23 04:40

minimum SDK API 8 : Android 2.2(Froyo) 使っている実機はNexus7 (Android 4.3,API 18)です。 よろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問