2016-02-06 71 views
-4

这里是我的MapsActivity.java文件,我已经使用了RequestPermission回调以及所有对话框,要求我授予位置权限不会出现,并且应用程序崩溃! package saksham.geofencing;Android M的Android权限不起作用

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mGeofences = new ArrayList<Geofence>(); 
     mGeofenceCoordinates = new ArrayList<LatLng>(); 
//  double string0=prefenceSettings.getString("LatLng0", "34"); 

     setContentView(R.layout.activity_maps); 
     showHelpForFirstLaunch(); 
     SupportMapFragment supportMapFragment = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); 
     cd = new ConnectionDetector(getApplicationContext()); 
     isInternetPresent = cd.isConnectingToInternet(); 

     // check for Internet status 
     if (isInternetPresent) { 
      // Internet Connection is Present 
      // make HTTP requests 
        } else { 
      // Internet connection is not present 
      // Ask user to connect to Internet 
      showAlertDialog(MapsActivity.this, "No Internet Connection", 
        "You don't have internet connection."); 
     } 
     /** 
     * SupportMapFragment belongs to the v4 support library, contrary to the default MagFragment that is a native component in Android. 
     SupportMapFragment will support more Android versions, but it is also an additional library you have to add in your project, 
     so I think it really depends on the Android versions you are targeting: 
     • On recent versions, the default components should be enough 
     • On older versions you will need to install the v4 support library and maybe others 
     * 
     */ 
     db = openOrCreateDatabase("geofence", Context.MODE_PRIVATE, null); 
     db.execSQL("CREATE TABLE IF NOT EXISTS Coordinates(id INTEGER,latitude DOUBLE,longitude DOUBLE,true VARCHAR);"); 
     db.execSQL("INSERT INTO Coordinates VALUES(0,0,0,'0')"); 
     db.execSQL("INSERT INTO Coordinates VALUES(1,0,0,'0')"); 
     db.execSQL("INSERT INTO Coordinates VALUES(2,0,0,'0')"); 
     googleMap = supportMapFragment.getMap(); 
     Log.i("My activity", "maps=" + googleMap); 
     googleMap.setMyLocationEnabled(true); 
     /** 
     * setMyLocationEnabled(true/false) shows the true location when the GPS is switched on from the device. It is an inbuilt feature of the googlemaps . 
     */ 

     LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE); 
     // getting GPS status 
     boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if(!isGPSEnabled) 
     { 
      Utils.displayPromptForEnablingGPS(this); 
     } 
     Log.i("My activity", "gps is" + isGPSEnabled); 

     // getting network status 
     boolean isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     Log.i("My activity", "network is" + isNetworkEnabled); 

     Criteria crta = new Criteria(); 
     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { 
      crta.setAccuracy(Criteria.ACCURACY_FINE); 
     } else { 
      crta.setAccuracy(Criteria.ACCURACY_MEDIUM); 
     } 
     /** 
     * we have used .setAccuracy as fine for higher SDks than gingerbread .Gingerbread is used as a reference because in apks lower 
     * than gingerbread there is very poor geo-fencing, with gingerbread google made it a lot easier for location services to be used for devleopers. 
     * it had improved set of tools for Location Services, which included geofencing and substantially improved location discovery. 
     */ 
     crta.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = locationManager.getBestProvider(crta, true); 

     /** 
     * It request Location updates after every 5 sec or if the user traveled 10m 
     */ 
     Log.i("My activity", "manager is " + locationManager); 
     Log.i("My activity", "provider is " + provider); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       MapsActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_COARSE_LOCATION}, 100); 

       return; 
      } 
     } 
     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this) 
        .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this) 
        .addApi(LocationServices.API) 
        .build(); 
      mGoogleApiClient.connect(); 
      Log.i("Api is", "" + mGoogleApiClient); 
     } 
     mLocationRequest = new LocationRequest(); 
     // We want a location update every 10 seconds. 
     mLocationRequest.setInterval(10000); 
     // We want the location to be as accurate as possible. 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 


     insertgeofence(); 


    } 
    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case 100: { 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Toast.makeText(this, "Thanks for the permission", Toast.LENGTH_LONG).show(); 
        // permission was granted, yay! do the 
        // calendar task you need to do. 
       } else { 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(this, "You did not allow to access your current location", Toast.LENGTH_LONG).show(); 
       } 
      } 
      // other 'switch' lines to check for other 
      // permissions this app might request 
     } 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     latitude=location.getLatitude(); 
     longitude=location.getLongitude(); 
     CameraPosition INIT = 
       new CameraPosition.Builder() 
         .target(new LatLng(latitude, longitude)) 
         .zoom(17.5F) 
         .bearing(300F) // orientation 
         .tilt(50F) // viewing angle 
         .build(); 
     // use GooggleMap mMap to move camera into position 
     googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(INIT)); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Location mlastlocation; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       MapsActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); 

       return; 
      } 
     } 
     mlastlocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     startLocationUpdates(); 
     if (mlastlocation != null) { 
      Log.i("the last location:", "" + mlastlocation); 
//   Toast.makeText(this, "Get last location first asshole!", Toast.LENGTH_LONG).show(); 
     } 


    } 


    protected void startLocationUpdates() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       MapsActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100); 

       return; 
      } 
      LocationRequest mLocationRequest = new LocationRequest(); 

      LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, this); 
     } 
    } 

    } 
+0

您的权限检查代码和位置代码之间没有任何关联,您只是将其视为形式。您需要在写入任何位置代码之前检查权限。 –

+0

你能给我举一个例子吗? –

回答

0

在检查权限之前,您正在调用googleMap.setMyLocationEnabled(true)。此外,您应该在检查权限后实例化任何位置代码。此外,在您的onRequestPermissionsResult中,您可能需要添加您想要启动的位置代码。

+0

按位置代码,你的意思是reqPermission代码?因为它已经被赋值为200的全局值,并且被访问过一次!感谢您的答案,虽然它的作品! –

+0

不,我的意思是您希望在授予权限后运行的代码。您可能希望将所有需要完成的代码放到一个单独的函数中,这样如果您已经拥有权限,您可以轻松地从“onCreate”调用,或者如果您需要授予它,可以从“onRequestPermissionsResult”调用。 –