В моем приложении настроена кнопка загрузки. Когда я нажимаю кнопку загрузки, когда inte rnet недоступен, нажимается кнопка. Когда inte rnet доступен, эта активность загрузки начинается, и файл загружается не раз, когда я щелкал, когда inte rnet был в автономном режиме.
Есть ли способ отключить загрузку, когда inte rnet недоступен, давая сообщение, проверьте ваше inte rnet соединение '.
MainActivity
public class Download_Activity extends AppCompatActivity {
FirebaseFirestore downdb;
RecyclerView downRecyclerView;
ArrayList<Downloadsmodel> downloadsmodelArrayList=new ArrayList<>();
DownloadsAdapter downloadsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_);
setUpRV();
setUpFB();
dataFromFirebase();
}
private void dataFromFirebase() {
downdb.collection("Downloads").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot documentSnapshot: task.getResult()){
Downloadsmodel downloadsmodel= new Downloadsmodel(documentSnapshot.getString("name")
,documentSnapshot.getString("link"),documentSnapshot.getString("fileextension"),documentSnapshot.getString("uploaddate"),documentSnapshot.getString("icon"));
downloadsmodelArrayList.add(downloadsmodel);
}
downloadsAdapter= new DownloadsAdapter(Download_Activity.this,downloadsmodelArrayList);
downRecyclerView.setAdapter(downloadsAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Download_Activity.this,"Error", Toast.LENGTH_SHORT).show();
}
});
}
private void setUpFB() {
downdb=FirebaseFirestore.getInstance();
}
private void setUpRV() {
downRecyclerView=findViewById(R.id.drecyclerdownloads);
downRecyclerView.setHasFixedSize(true);
downRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Скачать адаптер
public class DownloadsAdapter extends RecyclerView.Adapter<Downloadsviewholder> {
Download_Activity download_activity;
ArrayList<Downloadsmodel> downloadsmodels;
public DownloadsAdapter(Download_Activity download_activity, ArrayList<Downloadsmodel> downloadsmodels) {
this.download_activity = download_activity;
this.downloadsmodels = downloadsmodels;
}
@NonNull
@Override
public Downloadsviewholder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(download_activity.getBaseContext());
View view = layoutInflater.inflate(R.layout.downloads_elements,null,false);
return new Downloadsviewholder(view);
}
@Override
public void onBindViewHolder(@NonNull final Downloadsviewholder downloadsviewholder, final int i) {
downloadsviewholder.dName.setText(downloadsmodels.get(i).getName());
downloadsviewholder.dUploaddate.setText(downloadsmodels.get(i).getUploaddate());
downloadsviewholder.dExtension.setText(downloadsmodels.get(i).getFileextension());
Picasso.get().load(downloadsmodels.get(i).getIcon()).into(downloadsviewholder.dIcon);
downloadsviewholder.dButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dowloadFile(downloadsviewholder.dName.getContext(),downloadsmodels.get(i).getName(),downloadsmodels.get(i).getFileextension()
,DIRECTORY_DOWNLOADS,downloadsmodels.get(i).getLink());
}
});
}
public void dowloadFile (Context context,String filename, String fileextension, String destinationdirectory, String url){
DownloadManager downloadManager=(DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse(url);
DownloadManager.Request request=new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationdirectory, filename+fileextension);
downloadManager.enqueue(request);
}
@Override
public int getItemCount() {
return downloadsmodels.size();
}
}