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.
どうか、よろしくお願いします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/29 00:25