本文介绍了在没有互联网的设备上隐藏Firebase上的ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种情况:$ b $ ul
$ b $ p
完美地工作,但问题当设备没有互联网连接,在这种情况下进展对话总是展出,我想做什么,当设备没有互联网是关闭进展,并显示一个错误信息。
这是我的代码:
showProgessDialog();
FirebaseDatabase数据库= FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference(hello);
DatabaseReference connectedRef = FirebaseDatabase.getInstance()。getReference(。info / connected);
$ b $ connectedRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
boolean connected = snapshot.getValue(Boolean.class) ;
if(connected){
Log.d(Constans.LOG,1.- datachange connected);
hideProgressDialog();
} else {
Log.d(Constans.LOG,2.- datachange disconnected);
showProgessDialog();
}
}
@Override
public void onCancelled(DatabaseError error){
System.err.println(3.- datachange cancelled);
}
});
myRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.d(Constans.LOG,4。 - myRef addListenerForSingleValueEvent onDataChange);
}
@Override
public void onCancelled(DatabaseError databaseError){
Log.d(Constans.LOG,5.- myRef addListenerForSingleValueEvent cancell);
}
});
$ b myRef.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
Log.d(Constans.LOG ,6.- myRef addValueEventListener onDataChange);
String value = dataSnapshot.getValue(String.class);
mAboutMe.setText(value);
}
@Override
public void onCancelled(DatabaseError error){
Log.d(Constans.LOG,7.- myRef addValueEventListener error+ error.getDetails());
}
});
正如您所看到的,所有被调用的方法在Log行中都有一个数字,方法调用顺序是:
$ block
$ 2
$ b $ $ $
$ b
当没有互联网被称为
When there is not internet is just called
I can not close the progress where I open it.
Any idea about what I can do it using the Firebase framework?
解决方案
You can check internet connection of device by using isConnected method of this class. Then you dismiss progress dialog when there is not internet connection
public class InternetConnectionDetector
{
private Context context;
public InternetConnectionDetector(Context context)
{
this.context = context;
}
public boolean isConnected()
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//There are changes after apk lollipop while detecting internet connection:
//if user's os newer then lollipop:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
for(Network mNetwork : networks)
{
networkInfo = connectivityManager.getNetworkInfo(mNetwork);
if(networkInfo.getState().equals(NetworkInfo.State.CONNECTED))
{
return true;
}
}
}
//if user's os older then lollipop:
else
{
//getAllNetworkInfo method work for only before API 19:
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if(info != null)
{
for(NetworkInfo anInfo : info)
{
if(anInfo.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
这篇关于在没有互联网的设备上隐藏Firebase上的ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!