【问题标题】:Why are AndroidManifest permissions not being set in my application为什么我的应用程序中没有设置 AndroidManifest 权限
【发布时间】:2015-12-08 06:23:51
【问题描述】:

我对 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.

我还遇到过其他几个人遇到问题的情况,但这些通常是由于错误放置 (java.lang.SecurityException: Requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission)、拼写错误 (ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted) 或权限字符串的大小写错误造成的。

这是我的 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>

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

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

更新:

这里是Activity的核心;我已经删除了一些不相关的方法(例如 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);
    }
}

【问题讨论】:

标签: java android permissions


【解决方案1】:

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

  1. Android - Unable to get the gps location on the emulator
  2. ACCESS_FINE_LOCATION permission error emulator only
  3. http://www.gitshah.com/2011/02/android-fixing-no-internet-connection.html

正如你在 cmets 中引用的那样 -

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

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 2018-06-06
    • 2012-04-03
    • 1970-01-01
    • 2019-04-17
    • 2010-11-06
    • 2015-08-29
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多