为什么AndroidManifest权限没有在我的应用程序设置 [英] Why are AndroidManifest permissions not being set in my application
问题描述
我是完全新的Android开发,但我已经与Java(和其他编程语言)工作过。
我使用的Android Studio开发我的应用程序和访问位置信息(的 http://developer.android.com/training/location/index.html )。据我所知,位置服务需要的 android.permission.ACCESS_COARSE_LOCATION
和/或 android.permission.ACCESS_FINE_LOCATION
权限,但在把它们添加到我的ApplicationManifest我的应用程序仍与一个SecurityException崩溃
java.lang.SecurityException异常:客户必须有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION的权限来执行任意位置操作。
我遇到的麻烦有几个人等情况,但这些一般都误配售的结果(<一个href=\"http://stackoverflow.com/questions/6466427/java-lang-securityexception-requires-access-fine-location-or-access-coarse-loca\">java.lang.SecurityException:需要ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION许可),错误拼写(<一个href=\"http://stackoverflow.com/questions/6656258/access-fine-location-androidmanifest-permissions-not-being-granted\">ACCESS_FINE_LOCATION AndroidManifest权限不被授予)或许可权字符串的不正确的大写。
下面是我的ApplicationManifest.xml
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=scd.tt&GT; &LT;使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/&GT;
&LT;使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/&GT;
&LT;使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/&GT;
&LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT; &LT;用途-SDK
安卓的minSdkVersion =14
机器人:targetSdkVersion =23/&GT; &lt;应用
机器人:allowBackup =真
机器人:图标=@的mipmap / ic_launcher_tt_v2
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ TT_Theme&GT;
&LT;活动
机器人:名字=。MainActivity
机器人:主题=@风格/ TT_Theme.NoActionBar&GT;
&所述;意图滤光器&gt;
&lt;作用机器人:名字=android.intent.action.MAIN/&GT;
&LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
&所述; /意图滤光器&gt;
&LT; /活性GT;
&LT;活动机器人:LoginActivityNAME =机器人:主题=@风格/ TT_Theme.NoActionBar/&GT;
&LT;活动机器人:名字=。IndexActivity/&GT;
&LT; /用途&gt;&LT; /清单&GT;
我甚至试图删除ACCESS_NETWORK_STATE和网上看到的,如果在应用程序需要与HTTP服务器(它成功地执行)进行通信,这将导致其他SecurityExceptions它会试图获取位置信息之前。
这好像修改我做出的AndroidManifest的权限,当我运行程序不被更新。我也试着清洁,并使用生成菜单重新生成应用程序。
更新:
下面是活动的核心;我已经删除了一些不相关的方法(如onCreateOptionsMenu()和onBack pressed(),因为他们什么都没有做的位置)
包scd.tt;进口android.app.AlertDialog;
进口android.app.Fragment;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.location.Location;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.api.GoogleApiClient;
进口com.google.android.gms.location.LocationListener;
进口com.google.android.gms.location.LocationRequest;
进口com.google.android.gms.location.LocationServices;进口的java.util.Map;/ **
*创建者利亚姆在2015年8月9日
* /
公共类IndexActivity扩展AppCompatActivity实现GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener的{ 私人GoogleApiClient mGoogleApiClient;
私人LocationRequest mLocationRequest;
私人位置mLastLocation; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_index);
如果(调试)Log.d(TAG的onCreate()呼吁:savedInstanceState是+(savedInstanceState == NULL空:不为空)); mLocationRequest =新LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
buildGoogleApiClient(); 如果(findViewById(R.id.index_fragment_container)!= NULL){
如果(savedInstanceState!= NULL)回报; 片段指数=新IndexFragment();
index.setArguments(getIntent()getExtras()); 。getFragmentManager()调用BeginTransaction()加(R.id.index_fragment_container,指数).commit();
}
}
@覆盖
保护无效调用onStart(){
super.onStart();
mGoogleApiClient.connect();
} @覆盖
保护无效的onStop(){
super.onStop();
mGoogleApiClient.disconnect();
} @覆盖
公共无效onConnected(束束){
如果(调试)Log.d(TAGonConnected);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
startLocationUpdates();
} @覆盖
公共无效onConnectionSuspended(int i)以{
如果(调试)Log.d(TAG,连接暂停);
} @覆盖
公共无效onConnectionFailed(ConnectionResult connectionResult){
如果(调试)Log.d(TAG,连接失败);
} @覆盖
公共无效onLocationChanged(地点){
mLastLocation =位置;
} 保护同步无效buildGoogleApiClient(){
mGoogleApiClient =新GoogleApiClient.Builder(本)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
.addApi(LocationServices.API)
。建立();
如果(调试)Log.d(TAGbuildGoogleAPIClient());
} 保护无效startLocationUpdates(){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,这一点);
}
}
检查这些3个环节。他们可能会帮助 -
- Android - 无法获取模拟器上的GPS定位
- ACCESS_FINE_LOCATION权限错误模拟器只
- http://www.gitshah.com/ 2011/02 / Android的固定,没有互联网connection.html
作为评论引述你 -
与危险保护级权限必须在运行时通过提示用户为SDK 23批出,而不能在清单仅仅定义了。
块引用>I'm completely new to Android development, but I have worked with Java (and other programming languages) before.
I'm using Android Studio to develop my application and I've become stuck when following the developer tutorial on accessing Location information (http://developer.android.com/training/location/index.html). I understand that the Location services require the
android.permission.ACCESS_COARSE_LOCATION
and/orandroid.permission.ACCESS_FINE_LOCATION
permissions but upon adding them to my ApplicationManifest my application still crashes with a SecurityExceptionjava.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
I've come across several other instances of people having trouble, but these have generally been the result of mis-placing (java.lang.SecurityException: Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission), mis-spelling (ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted) or incorrect capitalization of the permission string.
Here is my ApplicationManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="scd.tt" > <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher_tt_v2" android:label="@string/app_name" android:theme="@style/TT_Theme"> <activity android:name=".MainActivity" android:theme="@style/TT_Theme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LoginActivity" android:theme="@style/TT_Theme.NoActionBar" /> <activity android:name=".IndexActivity" /> </application> </manifest>
I've even tried to remove ACCESS_NETWORK_STATE and INTERNET to see if this would cause other SecurityExceptions as the application is required to communicate with an HTTP server (which it performs successfully) before it gets attempts to get Location information.
It seems as though modifications I make to the permissions on the AndroidManifest aren't being updated when I run the program. I've also tried to Clean and Rebuild the application using the Build menu.
UPDATE:
Here is the core of the Activity; I've removed some of the unrelated methods (such as onCreateOptionsMenu() and onBackPressed() as they have nothing to do with the Location)
package scd.tt; import android.app.AlertDialog; import android.app.Fragment; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.location.Location; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import java.util.Map; /** * Created by Liam on 08/09/2015 */ public class IndexActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest; private Location mLastLocation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); if(debug) Log.d(TAG, "onCreate() called: savedInstanceState is " + (savedInstanceState == null ? "null" : "not null")); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(1000); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); buildGoogleApiClient(); if (findViewById(R.id.index_fragment_container) != null) { if (savedInstanceState != null) return; Fragment index = new IndexFragment(); index.setArguments(getIntent().getExtras()); getFragmentManager().beginTransaction().add(R.id.index_fragment_container, index).commit(); } } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); mGoogleApiClient.disconnect(); } @Override public void onConnected(Bundle bundle) { if(debug) Log.d(TAG, "onConnected"); mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); startLocationUpdates(); } @Override public void onConnectionSuspended(int i) { if(debug) Log.d(TAG, "Connection Suspended"); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { if(debug) Log.d(TAG, "Connection Failed"); } @Override public void onLocationChanged(Location location) { mLastLocation = location; } protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); if(debug) Log.d(TAG, "buildGoogleAPIClient()"); } protected void startLocationUpdates() { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } }
解决方案Check these 3 links. They might help -
- Android - Unable to get the gps location on the emulator
- ACCESS_FINE_LOCATION permission error emulator only
- http://www.gitshah.com/2011/02/android-fixing-no-internet-connection.html
As quoted by you in comments -
Permissions with a protection level of dangerous must be granted at runtime by prompting the user as of SDK 23 and can not be defined solely in the Manifest any more.
这篇关于为什么AndroidManifest权限没有在我的应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!