为什么我的应用程序中没有设置AndroidManifest权限

Lia*_*eke 5 java permissions android

我是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: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
Run Code Online (Sandbox Code Playgroud)

我遇到过其他几个遇到麻烦的人,但这些都是错误放置的结果(java.lang.SecurityException:需要ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限),拼写错误(ACCESS_FINE_LOCATION AndroidManifest权限未被授予)或权限字符串的大小写不正确.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

我甚至尝试删除ACCESS_NETWORK_STATE和INTERNET以查看是否会导致其他SecurityExceptions,因为在尝试获取位置信息之前,应用程序需要与HTTP服务器(它成功执行)进行通信.

似乎我在运行程序时没有更新对AndroidManifest上的权限所做的修改.我还尝试使用Build菜单清理并重建应用程序.

更新:

这是活动的核心; 我已经删除了一些不相关的方法(例如onCreateOptionsMenu()和onBackPressed(),因为它们与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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Tec*_*iot 1

检查这 3 个链接。他们可能会有所帮助 -

  1. Android - 无法在模拟器上获取 GPS 位置
  2. 仅限 ACCESS_FINE_LOCATION 权限错误模拟器
  3. http://www.gitshah.com/2011/02/android-fixing-no-internet-connection.html

正如您在评论中引用的那样 -

从 SDK 23 开始,危险保护级别的权限必须在运行时通过提示用户来授予,并且不能再单独在清单中定义。