是否可以将上下文传递给外部非活动类来处理fusedclientlocation?

时间:2019-04-25 19:53:53

标签: android-context fusedlocationproviderclient

我试图对代码进行划分,因为它有点笨重,我有一个ActivityContainer可以在整个应用程序中显示,握住顶部和底部的导航栏,并只是在中间更改片段。我目前使用此类通过getFusedLocationProviderClient来设置用户位置。我想创建一个单独的类“ LocationHandler”来代替它,但是它需要上下文或活动。

这是LocationHandler

public LocationHandler(Context context){
        this.context = context;
    }

    private Context context;

    private DatabaseReference gRef = FirebaseDatabase.getInstance().getReference("data/geofire/");
    public FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
    final GeoFire geoFire = new GeoFire(gRef);
    private Location mLocation;
    private DatabaseReference iRef = FirebaseDatabase.getInstance().getReference("data").child("instruments").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    private static final int REQUEST_LOCATION = 0;
    private static final int REQUEST_LOCATION_PERMISSION = 0;

    @SuppressLint("MissingPermission")
    public void getLocation() {
        try {
                fusedLocationClient.getLastLocation()
                        .addOnSuccessListener(new OnSuccessListener<Location>() {
                            @Override
                            public void onSuccess(Location location) {
                                mLocation = location;
                                //     if (!mLocation.equals(null)) {
                                //Set Users location for retrieval
                                geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(mLocation.getLatitude(), mLocation.getLongitude()), new GeoFire.CompletionListener() {
                                    @Override
                                    public void onComplete(String key, DatabaseError error) {
                                        Log.e("Location", "Success");
                                    }
                                });
                                //   }
                                iRef.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                                            if (!ds.getValue().toString().equals("")) {
                                                //For each instrument user lists, add a location, duplicate data but denormalized.
                                                geoFire.setLocation(ds.getKey() + "/" + FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(mLocation.getLatitude(), mLocation.getLongitude()), new GeoFire.CompletionListener() {
                                                    @Override
                                                    public void onComplete(String key, DatabaseError error) {
                                                        Log.d("Instrument location Logged: ", "Success");
                                                    }
                                                });
                                            } else {
                                                DatabaseReference tRef = FirebaseDatabase.getInstance().getReference("data/geofire/" + ds.getKey())
                                                        .child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                                                tRef.removeValue();
                                            }
                                        }
                                    }
                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {
                                        //TODO: handle onCancelled
                                    }
                                });
                            }
                        });
        } catch (SecurityException e) {
            System.out.println("Error: " + e.toString());
        }
    }

这是我的ActivityContainer

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        Context context = getApplicationContext();
        lHandler = new LocationHandler(context);

我正在 'java.lang.RuntimeException:无法启动活动ComponentInfo {package / package.ActivityContainer}:java.lang.NullPointerException:不允许使用空上下文。”

我尝试了各种方式来传递活动和上下文而没有成功

0 个答案:

没有答案