为什么在API中没有调用FusedLocationApi(onLocationChanged)> 23?

时间:2017-03-14 13:22:57

标签: android location android-location fusedlocationproviderapi android-fusedlocation

我有一个获取位置的服务,这项服务在API上工作良好< 23但是在API> 23没有得到任何网络的位置。它只是让我的位置与GPS。

entityManager

我在API的清单中添加了这些权限> 23:

public class TrackLocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private static final long SYNCHRONIZATION_INTERVAL = 60 * 60 * 1000;
    private static boolean isServiceRunning;
    private static final String TAG = "SADASDQWREQWRwerwer";
    private int notificationId = 9999;
    private GoogleApiClient googleApiClient;

    private TrackLocationApplication app;
    private DataHelper dataHelper;

    public static boolean isServiceRunning() {
        return isServiceRunning;
    }

    private static void setIsServiceRunning(boolean isServiceRunning) {

        TrackLocationService.isServiceRunning = isServiceRunning;
        EventBus.getDefault().post(AppEvent.SERVICE_STATE_CHANGED);
    }

    public TrackLocationService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");

        app = (TrackLocationApplication) getApplication();
        dataHelper = DataHelper.getInstance(getApplicationContext());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        createGoogleApiClient();
        connectGoogleApiClient();

        TrackLocationService.setIsServiceRunning(true);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        stopLocationUpdates();
        cancelNotification();
        app.setStartLocation(null);

        TrackLocationService.setIsServiceRunning(false);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "onLocationChanged");
        if (app.getStartLocation() == null) {
            app.setStartLocation(location);
        }
        Log.i("ASDASDASDASDA", location.getLatitude() + "" + location.getLongitude());
        updateLocationData(location);
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected");
        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "onConnectionSuspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.d(TAG, "onConnectionFailed");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Log.d(TAG, "onTaskRemoved");
    }

    private void createGoogleApiClient() {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    private void connectGoogleApiClient() {
        if (googleApiClient != null) {
            if (!(googleApiClient.isConnected() || googleApiClient.isConnecting())) {
                googleApiClient.connect();
            } else {
                Log.d(TAG, "Client is connected");
                startLocationUpdates();
            }
        } else {
            Log.d(TAG, "Client is null");
        }
    }

    private void startLocationUpdates() {
        Log.i("ASDASDASDASDA", "1");
        LocationRequest locationRequest = app.createLocationRequest();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
        scheduleDataSynchronization();
    }

    private void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                googleApiClient, this);
        stopDataSynchronization();
    }

    private void scheduleDataSynchronization() {

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, TrackLocationSyncReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                SYNCHRONIZATION_INTERVAL, alarmIntent);

    }

    private void stopDataSynchronization() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, TrackLocationSyncReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        alarmManager.cancel(alarmIntent);
    }

    private void updateLocationData(Location location) {

        Location startLocation = app.getStartLocation();
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();


        float distance = Utils.distFromCoordinates((float) startLocation.getLatitude(),
                (float) startLocation.getLongitude(),
                (float) latitude,
                (float) longitude);

        String timeText = Utils.formatTime(System.currentTimeMillis());
        long date = System.currentTimeMillis();
        dataHelper.saveLocation(LocationData.getInstance(latitude, longitude, "2"));
        updateNotification(timeText);
    }


    private void updateNotification(String text) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(getString(R.string.qomGardy))
                        .setContentText(text);

        Intent resultIntent = new Intent(this, GetNumberActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(GetNumberActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = mBuilder.build();
        mNotificationManager.notify(notificationId, notification);
    }

    private void cancelNotification() {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notificationId);
    }

    private void synchronizeData() {
        new AsyncTask<Void, Void, TrackLocationResponse>() {
            private List<LocationData> locations;

            @Override
            protected TrackLocationResponse doInBackground(Void[] params) {
                TrackLocationResponse response = null;

                //locations = dataHelper.getLocationsToSync();
                if (locations != null && locations.size() > 0) {
                    String deviceId = TrackLocationPreferencesManager.getDeviceId(getApplicationContext());
                    String userName = TrackLocationPreferencesManager.getUserName(getApplicationContext());
                    TrackLocationRequest request = TrackLocationRequest.getInstance(locations, deviceId, userName);
                    ITrackLocationClient client = new TrackLocationClient();
                    response = client.addTrack(request);
                    if (response != null && response.getStatus() == TrackLocationResponse.RESPONSE_CODE_OK) {
                        Log.d("TrackLocationSync", "Synced " + locations.size() + " items");
                        //dataHelper.markLocationsSynced(locations);
                    }
                } else {
                    Log.d("TrackLocationSync", "No data to be synced");
                }
                return response;
            }

            @Override
            protected void onPostExecute(TrackLocationResponse response) {
                super.onPostExecute(response);

                if (response != null && response.getStatus() == TrackLocationResponse.RESPONSE_CODE_OK) {
                    String message = null;
                    List<FriendResult> results = response.getResult();
                    if (results != null && results.size() > 0) {
                        StringBuilder messageBuilder = new StringBuilder();
                        messageBuilder.append("Hi from ");
                        for (FriendResult r : results) {
                            messageBuilder.append(" ");
                            messageBuilder.append(r.getTitle());
                            messageBuilder.append(",");
                        }
                        messageBuilder.deleteCharAt(messageBuilder.length() - 1);
                        message = messageBuilder.toString();
                    } else {
                        message = "Sync " + locations.size() + " items at " + Utils.formatTime(System.currentTimeMillis());
                    }

                    updateNotification(message);
                }
            }
        }.execute();
    }

}

不要拨打<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />

在我的活动中,我获得了所有权限。

1 个答案:

答案 0 :(得分:1)

在API 23+上,您需要在运行时请求权限,而不是在清单中: Requesting Permissions at Run Time

编辑:在您发布的代码中,您不执行ActivityCompat.requestPermissions只检查权限,正如我在23+中所说的那样,您必须在运行时请求权限,如果您在其他地方执行权限,请添加代码也是如此。

PS:你只需要实现TODO:你有

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;}