mLocationRequest = new LocationRequest();错误,

时间:2018-04-24 11:51:44

标签: android firebase dictionary

错误日志: - Error Log 这是图片错误行Error Line

这个项目我正在使用firebase auth,recyclerview工作,,但得到纬度,经度误差,,,

公共类ListOnline扩展AppCompatActivity实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {

DatabaseReference onlineRef, currentUserRef, counterRef, locations;
FirebaseRecyclerAdapter<User, userlistonlineviewholder> adapter;

RecyclerView listoneline;
RecyclerView.LayoutManager layoutManager;


private static final int MY_PERMISSION_REQUEST_CODE = 7171;
private static final int PLAY_SERVICE_RES_REQUEST = 7172;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;


private static int UPDATE_INTERVEL = 5000;
private static int FASTEST_INTERVEL = 3000;
private static int DISTANCE = 10;


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

    listoneline = (RecyclerView) findViewById(R.id.onlinerecy);

    listoneline.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    listoneline.setLayoutManager(layoutManager);

    Toolbar toolbar = (Toolbar) findViewById(R.id.bar);
    toolbar.setTitle("Current Online Users");
    setSupportActionBar(toolbar);


    locations = FirebaseDatabase.getInstance().getReference("Locations");
    onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");
    counterRef = FirebaseDatabase.getInstance().getReference("lastonline");
    currentUserRef = FirebaseDatabase.getInstance().getReference("lastonline")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid());


    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{

                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSION_REQUEST_CODE);

    } else {

        if (checkplayservices()) {
            buildGoogleApiClient();
            createLocationRequest();
            displayLocation();
        }
    }

    SetupSystem();
    updatelist();

}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSION_REQUEST_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (checkplayservices()) {

                    buildGoogleApiClient();
                    createLocationRequest();
                    displayLocation();
                }
            }

        }
        break;
    }

}

private void displayLocation() {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        locations.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .setValue(new Tracking(FirebaseAuth.getInstance().getCurrentUser().getEmail(),
                        FirebaseAuth.getInstance().getCurrentUser().getUid(),
                        String.valueOf(mLastLocation.getLatitude()),
                        String.valueOf(mLastLocation.getLongitude())));
    } else {
        Toasty.error(getApplicationContext(), "Couldn't not find Location").show();

    }
}

private void createLocationRequest() {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVEL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVEL);
    mLocationRequest.setSmallestDisplacement(DISTANCE);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

}

private void buildGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();
    mGoogleApiClient.connect();


}

private boolean checkplayservices() {
    int resultcode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultcode != ConnectionResult.SUCCESS) {

        if (GooglePlayServicesUtil.isUserRecoverableError(resultcode)) {

            GooglePlayServicesUtil.getErrorDialog(resultcode, this, PLAY_SERVICE_RES_REQUEST).show();

        } else {
            Toasty.error(getApplicationContext(), "This Device is not Supported").show();
            finish();
        }
        return false;
    }
    return true;

}


private void updatelist() {

    FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
            .setQuery(counterRef, User.class)
            .build();

    adapter = new FirebaseRecyclerAdapter<User, userlistonlineviewholder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull userlistonlineviewholder holder, int position, @NonNull User model) {

            holder.textView.setText(model.getName());

        }

        @NonNull
        @Override
        public userlistonlineviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.userstatuslayout, parent, false);


            return new userlistonlineviewholder(view);
        }

    };
    adapter.notifyDataSetChanged();
    listoneline.setAdapter(adapter);
    adapter.startListening();

}


private void SetupSystem() {

    onlineRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            currentUserRef.onDisconnect().removeValue();
            counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                    .setValue(new User(FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), "Online"));
            adapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    counterRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postDatasnapsot : dataSnapshot.getChildren()) {
                User user = postDatasnapsot.getValue(User.class);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    new MenuInflater(this).inflate(R.menu.userbar, menu);
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_logout:
            currentUserRef.removeValue();
            break;

        case R.id.nav_join:
            counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                    .setValue(new User(FirebaseAuth.getInstance().getCurrentUser().getDisplayName(), "Online"));

            break;

    }


    return super.onOptionsItemSelected(item);
}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    displayLocation();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

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

    displayLocation();
    startLocationUpdates();

}

private void startLocationUpdates() {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}

@Override
public void onConnectionSuspended(int i) {

    mGoogleApiClient.connect();

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }

}

@Override
protected void onStop() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

@Override
protected void onResume() {
    super.onResume();
    checkplayservices();
}

}

0 个答案:

没有答案