简体   繁体   English

GoogleApiClient无法在棉花糖及以上设备中使用吗?

[英]GoogleApiClient is not working in marshmallow and above devices?

Am using googleapi client for getting user location it is working fine below marshmallow devices but on marshmallow devices application getting crashed don't know the reason can someone help me out let me post my code this is the activity am trying to get location: 我正在使用googleapi客户端获取用户位置,但在棉花糖设备下工作正常,但在棉花糖设备上应用程序崩溃了,不知道有人可以帮我的原因让我发布我的代码,这是该活动正在尝试获取位置:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.gcm.GcmNetworkManager;
import com.google.android.gms.gcm.PeriodicTask;
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.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;

import java.text.DateFormat;
import java.util.Date;

import static precisioninfomatics.backgroundgps.MyLocationService.TASK_GET_LOCATION_PERIODIC;

public class GPS extends Activity implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, GetMethod {
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private static final String TAG = "LocationActivity";
    private static final long INTERVAL = 1000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    Button btnFusedLocation;
    TextView tvLocation;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mCurrentLocation;
    String mLastUpdateTime;
    private AsyncTaskGet asyncTaskGet;
    protected static final int REQUEST_CHECK_SETTINGS = 0x1;

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate ...............................");
        if (!checkPlayServices()) {
            finish();
        }
        createLocationRequest();
        startService(new Intent(this, GPSService.class));
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        setContentView(R.layout.activity_gps);
        tvLocation = (TextView) findViewById(R.id.tvLocation);
       btnFusedLocation = (Button) findViewById(R.id.btnShowLocation);
        btnFusedLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mCurrentLocation != null) {
                    String lat = String.valueOf(mCurrentLocation.getLatitude());
                    String lng = String.valueOf(mCurrentLocation.getLongitude());
                    String userID = "1";
                    String time = String.valueOf(System.currentTimeMillis());
                    String url = "http://172.16.6.106:8080/gpstracker/api/coordinates/" + lat + "/" + lng + "/" + userID + "/" + time;
                    Log.d("url", url);
                    GetNoteList(getApplicationContext());
                    asyncTaskGet.execute(url);

                }
            }
        });
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
        builder.setAlwaysShow(true);
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                        builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        startLocationUpdates();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the user
                        // a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(
                                    GPS.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }
            }
        });
        startPeriodicLocationTask();

    }

    public void startPeriodicLocationTask() {
        Log.d("periodictask", "startPeriodicLocationTask");
        GcmNetworkManager mGcmNetworkManager = GcmNetworkManager.getInstance(this);
        PeriodicTask taskBuilder = new PeriodicTask.Builder()
                .setService(MyLocationService.class)
                .setTag(TASK_GET_LOCATION_PERIODIC)
                .setPeriod(30).setFlex(20)
                .setPersisted(true).build();
        mGcmNetworkManager.schedule(taskBuilder);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart fired ..............");
        mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop fired ..............");
        mGoogleApiClient.disconnect();
        Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
    }

    private boolean checkPlayServices() {
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
        int result = googleAPI.isGooglePlayServicesAvailable(this);
        if(result != ConnectionResult.SUCCESS) {
            if(googleAPI.isUserResolvableError(result)) {
                googleAPI.getErrorDialog(this, result,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            }

            return false;
        }

        return true;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
        startLocationUpdates();
    }

    protected void startLocationUpdates() {
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);

        Log.d(TAG, "Location update started ..............: ");
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

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

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "Firing onLocationChanged..............................................");
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }

    private void updateUI() {
        Log.d(TAG, "UI update initiated .............");
        if (null != mCurrentLocation) {
            String lat = String.valueOf(mCurrentLocation.getLatitude());
            String lng = String.valueOf(mCurrentLocation.getLongitude());
            tvLocation.setText("At Time: " + mLastUpdateTime + "\n" +
                    "Latitude: " + lat + "\n" +
                    "Longitude: " + lng + "\n" +
                    "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" +
                    "Provider: " + mCurrentLocation.getProvider());
        } else {
            Log.d(TAG, "location is null ...............");
        }
    }

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

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
        Log.d(TAG, "Location update stopped .......................");
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mGoogleApiClient.isConnected()) {
            startLocationUpdates();
            Log.d(TAG, "Location update resumed .....................");
        }
    }

    public void GetNoteList(Context context) {
        asyncTaskGet = new AsyncTaskGet(context);
        asyncTaskGet.getMethod = this;
    }

    @Override
    public Void getDataFromServer(String objects) {
        Log.d("response", objects);
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
// Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        startLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.d("nogps", "nogps");
                        break;
                }
                break;
        }
    }
}

My gradle: 我的gradle:

dependencies {

    compile 'com.google.android.gms:play-services:9.8.0'

     testCompile 'junit:junit:4.12'
}

I think am making mistake in googleplay service am using late googleplay service version can somebody help me to solve this issue!! 我认为在googleplay服务中出现了错误,正在使用较晚的googleplay服务版本,有人可以帮助我解决此问题!!

Runtime permissions are your issue, in Android M and above google added the need to request for permissions when they are needed (like in iOS). 运行时权限是您的问题,在Android M及更高版本中,google添加了在需要权限时(例如在iOS中)请求权限的需求。

See this link: https://developer.android.com/training/permissions/requesting.html 看到此链接: https : //developer.android.com/training/permissions/requesting.html

There are many wrappers around to make adding permissions easier on sites like https://android-arsenal.com/tag/235?category=1 有很多包装器可以使在https://android-arsenal.com/tag/235?category=1等网站上添加权限更加容易

Here is some code, taken from android developers site to help you along: 这是一些代码,摘自android开发人员网站,可帮助您:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

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

    // Show an expanation 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(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 showInputMethodPicker 在 Marshmallow 以上的设备上不起作用? - showInputMethodPicker not working on devices above Marshmallow? 相机许可在棉花糖及以上设备上不起作用并崩溃 - Camera permission is not working on Marshmallow and above devices and crashes 在棉花糖上方的设备中以编程方式在Android中断开通话 - to disconnect the call programmatically in android for devices above Marshmallow 无法在Android设备(在MarshMallow及更高版本)上创建目录 - Failed to create directory in Android (on MarshMallow and above) devices 从棉花糖及以上棉花糖设备上的onCreate中读取外部存储中的文件 - Read files from external storage in onCreate on marshmallow and above marshmallow devices 通话记录不适用于棉花糖及以上版本 - Call recording not working in marshmallow and above SmsManager在某些设备中不起作用(棉花糖+) - SmsManager is not working in some devices (Marshmallow +) 作物功能适用于棉花糖或以上,但不适用于低于棉花糖 - Crop functionality is working on marshmallow or above but do not work below marshmallow ChatHead服务在Nexus设备上的Lollipop和Marshmallow中不起作用 - ChatHead Service is Not Working in Lollipop and Marshmallow on Nexus devices javax.mail在棉花糖或更高版本的系统上不起作用 - javax.mail isn't working on Marshmallow or above systems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM