在运行时请求权限 - Android

时间:2016-08-11 14:22:15

标签: android

我尝试使用 Google Play 服务位置创建一个演示应用。它在我的手机上工作正常,但它会在短时间内显示位置。但是,模拟器没有显示任何位置。任何建议都表示赞赏..

模拟器的当前版本 - 6.0 targetSdkVersion 24

这是MainActivity Code

package com.example.android.mylocation;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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;

import java.io.IOException;
import java.util.jar.Manifest;
import android.Manifest.permission;

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

    private final String LOG_TAG = "MyLocation";
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private TextView mTextView;
    public static final int MY_PERMISSION_REQUEST_LOCATION = 1;

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

        /** Create Google Api Client */
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        Log.v(LOG_TAG, "Google Api Client Created");

        mTextView = (TextView) findViewById(R.id.location);
    }

    /** This method will connect the Google Api Client */
    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();

        Log.v(LOG_TAG, "Connect the Google Play Services");
    }

    /** This method will disconnect the Google Api Client */
    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        Log.v(LOG_TAG, "Google Play Services Connected");

        try{
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            Log.v(LOG_TAG, "Priority High Accuracy");

            mLocationRequest.setInterval(5000);

            /** Request the permissions you need */
            if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

                Log.v(LOG_TAG, "Self Permission is not Granted");
                //Request the permission
                ActivityCompat.requestPermissions(this, new String[] {permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_LOCATION);
            }
        }
        catch(Exception e){
            Log.v(LOG_TAG, e.toString());
        }





/*        try {
            //mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        }
        catch(SecurityException e){
            Log.v(LOG_TAG, e.toString());
        }*/
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        Log.v(LOG_TAG, "User permission");

        switch (requestCode) {
            case MY_PERMISSION_REQUEST_LOCATION: {

                Log.v(LOG_TAG, "Check the User Response");

                //If request is cancelled, the request arrays are empty
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    try {
                        Log.v(LOG_TAG, "Check the user location");

                        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                        //mTextView.setText(location.toString());

                    }
                    catch(SecurityException e){
                        Log.v(LOG_TAG, e.toString());
                    }
                }
                else {
                    Log.v(LOG_TAG, "Permission denied");
                }
                return;
            }
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        /*if (!mGoogleApiClient.isConnected()){
            mGoogleApiClient.connect();
        }*/
        Log.v(LOG_TAG, "Connection Suspended");
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.v(LOG_TAG, connectionResult.getErrorMessage());
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.v(LOG_TAG, "Location Change Detected");

        mTextView.setText(location.toString());
    }
}

0 个答案:

没有答案