如何在firebase firestore中存储textclock

cig3rfwq  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(332)

我想把textclock存储在firebase firestore上,已经试了将近一周的时间来解决它们,但是仍然没有找到。我正在为我的最后一年的项目做一个考勤系统,这个代码应该在用户单击上一个活动中的按钮后返回值(用户id、名称、时间和位置)并存储在firebase firestore中
这是我做的代码

public class DetailsKehadiran extends AppCompatActivity {

TextView tvNama, tvID, tvWaktu, tvLocation;
FusedLocationProviderClient fusedLocationProviderClient;
FirebaseAuth firebaseAuth;
FirebaseFirestore fStore;
TextClock textClock;
String userId;

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

    firebaseAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();
    userId = firebaseAuth.getCurrentUser().getUid();

    tvID = findViewById(R.id.tvID);
    tvNama = findViewById(R.id.tvNama);
    tvWaktu = findViewById(R.id.tvWaktuMasuk);
    tvLocation = findViewById(R.id.tvLocation);
    textClock = findViewById(R.id.textClock);

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    if (ActivityCompat.checkSelfPermission(DetailsKehadiran.this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(DetailsKehadiran
    .this,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        getLocation();
    } else {
        //when permission is denied
        //request permission
        ActivityCompat.requestPermissions(DetailsKehadiran.this, new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}
                ,100);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //check condition
    if (requestCode == 100 && grantResults.length > 0 && (grantResults[0] + grantResults[1]
    == PackageManager.PERMISSION_GRANTED)){
        getLocation();
    }else{
        Toast.makeText(getApplicationContext(), "Permintaan ditolak.",Toast.LENGTH_SHORT).show();
    }
}

@SuppressLint("MissingPermission")
private void getLocation() {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //check condition
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

        fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                Location location = task.getResult();
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (location != null) {

                    try {
                        //initialize geocoder
                        Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                        //initialize address list
                        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        //set address on TextView
                        //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));

                        DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                        Map<String,Object> userInfo = new HashMap<>();
                        userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                        //userInfo.put("WaktuMasuk", Timestamp.now());
                        documentReference.update(userInfo);

                        documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                            @Override
                            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                tvNama.setText(documentSnapshot.getString("FullName"));
                                tvLocation.setText(documentSnapshot.getString("Location"));
                                tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else{
                    LocationRequest locationRequest= new LocationRequest()
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setInterval(10000)
                            .setFastestInterval(1000)
                            .setNumUpdates(1);

                    LocationCallback locationCallback = new LocationCallback() {
                        @Override
                        public void onLocationResult(LocationResult locationResult) {

                            Location location1 = locationResult.getLastLocation();
                            try {
                                //initialize geocoder
                                Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                                //initialize address list
                                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                                DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                                //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));
                                Map<String,Object> userInfo = new HashMap<>();
                                userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                                //userInfo.put("WaktuMasuk",Timestamp.now());
                                documentReference.update(userInfo);

                                documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                                    @Override
                                    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                        tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                        tvNama.setText(documentSnapshot.getString("FullName"));
                                        tvLocation.setText(documentSnapshot.getString("Location"));
                                        tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                                    }
                                });

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    fusedLocationProviderClient.requestLocationUpdates(locationRequest
                    ,locationCallback, Looper.myLooper());
                }
            }
        });
    }

    else{
        //when location service is not enabled
        //open location settings
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

顺便说一句,我正在做一个出勤功能,提前谢谢你!

vjrehmav

vjrehmav1#

您可以将timeclock值作为字符串存储在firebase中,方法是通过上一个活动的按钮上的textclock.gettext()提取文本值作为触发点。

相关问题