我完全不熟悉Android開發,但之前我曾使用過Java(以及其他編程語言)。爲什麼我的應用程序中沒有設置AndroidManifest權限
我正在使用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>
我甚至試圖刪除許可權和網上看到的,如果在應用程序需要與HTTP服務器進行通信,這將導致其他SecurityExceptions(它執行成功),然後它試圖獲取位置信息。
看來好像我對AndroidManifest的權限所做的修改在我運行程序時沒有被更新。我也嘗試使用Build菜單清理和重建應用程序。
UPDATE:
這裏是活動的核心;我已經刪除了一些不相關的方法(如onCreateOptionsMenu()和onBackPressed(),因爲他們什麼都沒有做的位置)
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);
}
}
您是否在模擬器上運行此程序? – Actiwitty
到目前爲止,您的活動中有哪些代碼? – fractalwrench
是的,應用程序正在模擬器中運行以進行測試。它基於使用API/SDK版本23的x86平臺上的Nexus 5。 –