0

我只是想要按照本教程(http://blog.teamtreehouse.com/beginners-guide-location-android)。我也向AndroidManifest添加了必需的權限。但是當我運行模擬器的應用程序時,它會停止。在我的logcat它說,Access_fine_location的權限丟失;但是這已經在清單中添加了。請有人幫我找到問題嗎?不幸的應用程序已停止+谷歌地圖+無法確定Access_Fine_Location

logcat的

09-13 12:31:31.768 7900-7900/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.scibdisrat.myapplication2, PID: 7900 
    java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations. 
      at android.os.Parcel.readException(Parcel.java:1599) 
      at android.os.Parcel.readException(Parcel.java:1552) 
      at com.google.android.gms.location.internal.zzg$zza$zza.zza(Unknown Source) 
      at com.google.android.gms.location.internal.zzi.zza(Unknown Source) 
      at com.google.android.gms.location.internal.zzj.zza(Unknown Source) 
      at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source) 
      at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source) 
      at com.google.android.gms.common.api.zzc$zza.zzb(Unknown Source) 
      at com.google.android.gms.common.api.zzf.zza(Unknown Source) 
      at com.google.android.gms.common.api.zzf.zzb(Unknown Source) 
      at com.google.android.gms.common.api.zzi.zzb(Unknown Source) 
      at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source) 
      at com.example.scibdisrat.myapplication2.MapsActivity.onConnected(MapsActivity.java:132) 
      at com.google.android.gms.common.internal.zzk.zzh(Unknown Source) 
      at com.google.android.gms.common.api.zzg.zznt(Unknown Source) 
      at com.google.android.gms.common.api.zzg.onConnected(Unknown Source) 
      at com.google.android.gms.common.api.zzi$2.onConnected(Unknown Source) 
      at com.google.android.gms.common.internal.zzj$zzg.zzoD(Unknown Source) 
      at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source) 
      at com.google.android.gms.common.internal.zzj$zza.zzs(Unknown Source) 
      at com.google.android.gms.common.internal.zzj$zzc.zzoF(Unknown Source) 
      at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-13 12:31:31.873 7900-7911/? I/art﹕ Background sticky concurrent mark sweep GC freed 4802(565KB) AllocSpace objects, 4(192KB) LOS objects, 0% free, 5MB/5MB, paused 2.786ms total 344.385ms 
09-13 12:31:31.924 1307-1906/? W/ActivityManager﹕ Force finishing activity com.example.scibdisrat.myapplication2/.MapsActivity 
09-13 12:31:31.936  955-955/? D/gralloc﹕ Registering a buffer in the process that created it. This may cause memory ordering problems. 
09-13 12:31:31.936  955-955/? E/libEGL﹕ called unimplemented OpenGL ES API 
09-13 12:31:31.936  955-955/? E/SurfaceFlinger﹕ glCheckFramebufferStatusOES error 1121825449 
09-13 12:31:31.936  955-955/? E/SurfaceFlinger﹕ got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot 
09-13 12:31:32.161 1892-1892/? I/Choreographer﹕ Skipped 193 frames! The application may be doing too much work on its main thread. 
09-13 12:31:32.452 1307-1321/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{f2b5723 u0 com.example.scibdisrat.myapplication2/.MapsActivity t17 f} 
09-13 12:31:32.684 1307-1326/? I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread. 
09-13 12:31:32.852 1307-1326/? I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread. 
09-13 12:31:32.903 1307-1326/? I/Choreographer﹕ Skipped 39 frames! The application may be doing too much work on its main thread. 
09-13 12:31:42.833 1307-1321/? W/ActivityManager﹕ Activity destroy timeout for ActivityRecord{f2b5723 u0 com.example.scibdisrat.myapplication2/.MapsActivity t17 f} 
09-13 12:34:40.558 1307-1317/? I/art﹕ WaitForGcToComplete blocked for 9.904ms for cause Background 
09-13 12:36:26.282 1307-1318/? I/ActivityManager﹕ Killing 6356:android.process.media/u0a5 (adj 15): empty for 1806s 
09-13 12:36:31.964 7900-7900/? I/Process﹕ Sending signal. PID: 7900 SIG: 9 
09-13 12:36:31.968 1307-1417/? I/ActivityManager﹕ Process com.example.scibdisrat.myapplication2 (pid 7900) has died 
09-13 12:36:31.991 1307-1463/? W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 7900 uid 10057 
09-13 12:36:32.040 1892-1892/? I/Choreographer﹕ Skipped 34 frames! The application may be doing too much work on its main thread. 
09-13 12:36:32.090 1307-1320/? W/ProcessCpuTracker﹕ Skipping unknown process pid 7960 
09-13 12:36:32.238 1307-1326/? W/AppOps﹕ Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0 
09-13 12:39:32.768 1307-1323/? E/BluetoothAdapter﹕ Bluetooth binder is null 
09-13 12:39:32.796 1307-1323/? E/KernelCpuSpeedReader﹕ Failed to read cpu-freq 
    java.io.FileNotFoundException: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory) 
      at libcore.io.IoBridge.open(IoBridge.java:452) 
      at java.io.FileInputStream.<init>(FileInputStream.java:76) 
      at java.io.FileInputStream.<init>(FileInputStream.java:103) 
      at java.io.FileReader.<init>(FileReader.java:66) 
      at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49) 
      at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8003) 
      at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1364) 
      at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.os.HandlerThread.run(HandlerThread.java:61) 
      at com.android.server.ServiceThread.run(ServiceThread.java:46) 
    Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 
      at libcore.io.Posix.open(Native Method) 
      at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
      at libcore.io.IoBridge.open(IoBridge.java:438) 
            at java.io.FileInputStream.<init>(FileInputStream.java:76) 
            at java.io.FileInputStream.<init>(FileInputStream.java:103) 
            at java.io.FileReader.<init>(FileReader.java:66) 
            at com.android.internal.os.KernelCpuSpeedReader.readDelta(KernelCpuSpeedReader.java:49) 
            at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8003) 
            at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1364) 
            at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.os.HandlerThread.run(HandlerThread.java:61) 
            at com.android.server.ServiceThread.run(ServiceThread.java:46) 
09-13 12:39:32.796 1307-1323/? E/KernelUidCpuTimeReader﹕ Failed to read uid_cputime 
    java.io.FileNotFoundException: /proc/uid_cputime/show_uid_stat: open failed: ENOENT (No such file or directory) 
      at libcore.io.IoBridge.open(IoBridge.java:452) 
      at java.io.FileInputStream.<init>(FileInputStream.java:76) 
      at java.io.FileInputStream.<init>(FileInputStream.java:103) 
      at java.io.FileReader.<init>(FileReader.java:66) 
      at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71) 
      at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8032) 
      at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1364) 
      at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.os.HandlerThread.run(HandlerThread.java:61) 
      at com.android.server.ServiceThread.run(ServiceThread.java:46) 
    Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 
      at libcore.io.Posix.open(Native Method) 
      at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
      at libcore.io.IoBridge.open(IoBridge.java:438) 
            at java.io.FileInputStream.<init>(FileInputStream.java:76) 
            at java.io.FileInputStream.<init>(FileInputStream.java:103) 
            at java.io.FileReader.<init>(FileReader.java:66) 
            at com.android.internal.os.KernelUidCpuTimeReader.readDelta(KernelUidCpuTimeReader.java:71) 
            at com.android.internal.os.BatteryStatsImpl.updateCpuTimeLocked(BatteryStatsImpl.java:8032) 
            at com.android.server.am.BatteryStatsService.updateExternalStats(BatteryStatsService.java:1364) 
            at com.android.server.am.BatteryStatsService$BatteryStatsHandler.handleMessage(BatteryStatsService.java:125) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:148) 
            at android.os.HandlerThread.run(HandlerThread.java:61) 
            at com.android.server.ServiceThread.run(ServiceThread.java:46) 
09-13 12:40:36.429 1307-1307/? I/EntropyMixer﹕ Writing entropy... 
09-13 12:41:00.127 1307-1321/? I/ProcessStatsService﹕ Prepared write state in 81ms 

MapsActivity.java

import android.content.IntentSender; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 

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 com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    public static final String TAG = MapsActivity.class.getSimpleName(); 

    /* 
    * Define a request code to send to Google Play services 
    * This code is returned in Activity.onActivityResult 
    */ 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    /** 
    * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
    * installed) and the map has not already been instantiated.. This will ensure that we only ever 
    * call {@link #setUpMap()} once when {@link #mMap} is not null. 
    * <p/> 
    * If it isn't installed {@link SupportMapFragment} (and 
    * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
    * install/update the Google Play services APK on their device. 
    * <p/> 
    * A user can return to this FragmentActivity after following the prompt and correctly 
    * installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
    * have been completely destroyed during this process (it is likely that it would only be 
    * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
    * method in {@link #onResume()} to guarantee that it will be called. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    /** 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, we 
    * just add a marker near Africa. 
    * <p/> 
    * This should only be called once and when we are sure that {@link #mMap} is not null. 
    */ 
    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 

     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 

     LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

     //mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location")); 
     MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .title("I am here!"); 
     mMap.addMarker(options); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
     else { 
      handleNewLocation(location); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     /* 
     * Google Play services can resolve some errors it detects. 
     * If the error has a resolution, try sending an Intent to 
     * start a Google Play services activity that can resolve 
     * error. 
     */ 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
       /* 
       * Thrown if Google Play services canceled the original 
       * PendingIntent 
       */ 
      } catch (IntentSender.SendIntentException e) { 
       // Log the error 
       e.printStackTrace(); 
      } 
     } else { 
      /* 
      * If no resolution is available, display a dialog to the 
      * user with the error. 
      */ 
      Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     handleNewLocation(location); 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.*******.myapplication2" > 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 

    <!-- 
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but are recommended. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="@string/google_maps_key" /> 

     <activity 
      android:name=".MapsActivity" 
      android:label="@string/title_activity_maps" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

回答

3

從SDK 23開始,您需要在運行時請求權限。您可以在此閱讀有關運行時權限的信息: http://developer.android.com/training/permissions/requesting.html

簡而言之,在編寫訪問位置服務的邏輯之前,您需要檢查應用程序是否具有所需的權限。在這種情況下,ACCESS_FINE_LOCATION是一個危險的權限。所以用戶必須明確授予權限。向用戶解釋爲什麼需要許可是一種很好的做法。用戶響應請求後,查明權限是否被授予,然後進行相應處理。繼續使用需要用戶位置的代碼或禁用它。

0

嗯,我已經解決了我的問題。由於ACCESS_FINE_LOCATION在sdk版本23中存在一些問題,因此我選擇了一個較低的目標SDK。

相關問題