Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Locations: Probably should be updated to require permissions at runtime on Android 6+ #1

Open
dmytroKarataiev opened this issue Apr 3, 2016 · 5 comments

Comments

@dmytroKarataiev
Copy link

I can send a pull request with additional code if needed and then you can check it/merge it.

@dmytroKarataiev dmytroKarataiev changed the title Probably should be updated to require permissions at runtime on Android 6+ Locations: Probably should be updated to require permissions at runtime on Android 6+ Apr 3, 2016
@jeetupal2806
Copy link

I using a MockLocation to test the code, but notification is not coming.

@MelanieJae
Copy link

Hello: I am in the process of doing a pull request for this but in order to test that the permission request and grant/denial are working correctly, I had to update the target version in each LocationLesson module build.gradle files and these files are in the master .gitignore for the repo. How should I handle the commit/push in this case? Melanie

@DoraNemet
Copy link

@JeetuPal did you figure it out?

@luizsilveira
Copy link

You can find information on how to request permission for Android 6.0+ (API level 23+) here:
https://developer.android.com/training/permissions/requesting.html

@luizsilveira
Copy link

luizsilveira commented Jul 8, 2017

This is my MainActivity.java with the added permissions:

package com.example.android.location2_1;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

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;

public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

protected static final String TAG = "Location Services Lesson 2-1";
private static final int CALLBACK_NUMBER = 1;
protected GoogleApiClient mGoogleApiClient;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
protected LocationRequest mLocationRequest;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLatitudeText = (TextView) findViewById(R.id.latitude_text);
    mLongitudeText = (TextView) findViewById(R.id.longitude_text);
    buildGoogleApiClient();

    int permissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    CALLBACK_NUMBER);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case CALLBACK_NUMBER: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

}

@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, location.toString());

    mLatitudeText.setText(String.valueOf(location.getLatitude()));
    mLongitudeText.setText(String.valueOf(location.getLongitude()));

}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Connection failed:" + connectionResult.getErrorCode());

}

public void onDisconnected() {
    Log.i(TAG, "Disconnected");
}

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

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants