【发布时间】:2019-02-21 11:42:02
【问题描述】:
我正在做一个我必须做的项目
配置实时互联网连接状态
一个客户端应用程序,即使应用程序在后台。
我必须显示它
天气特定设备连接到互联网的服务器端或 不是
我正在使用 Firebase 执行此场景,但它不起作用。
连接服务
public class ConnectionService extends Service implements ConnectivityReciever.ConnectivityRecieverListner {
public static final int notify = 5000;
private Handler mHandler=new Handler();
private Timer mTimer = null;
FirebaseDatabase db;
DatabaseReference dbRef;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
dbRef=FirebaseDatabase.getInstance().getReference(Common.DEVICE_NAME).child("online");
MyApplication.getInstance().setConnectivtyListner(ConnectionService.this);
boolean isConnected=ConnectivityReciever.isConnected();
checkConnection(isConnected);
if(mTimer!=null){
mTimer.cancel();
}else{
mTimer=new Timer();
mTimer.scheduleAtFixedRate(new TimeDisplay(),0,notify);
}
}
private void checkConnection(boolean isConnected) {
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(ConnectionService.this, "Service is destroyed", Toast.LENGTH_SHORT).show();
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
if(isConnected){
dbRef.onDisconnect().setValue("true");
Toast.makeText(ConnectionService.this, "online", Toast.LENGTH_SHORT).show();
}
if(!isConnected){
dbRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
Toast.makeText(ConnectionService.this, "offline", Toast.LENGTH_SHORT).show();
}
}
//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
MyApplication.getInstance().setConnectivtyListner(ConnectionService.this);
boolean isConnected=ConnectivityReciever.isConnected();
checkConnection(isConnected);
// display toast
Toast.makeText(ConnectionService.this, "Service is running", Toast.LENGTH_SHORT).show();
}
});
}
private void checkConnection(boolean isConnected) {
if(isConnected){
dbRef.setValue("true");
Toast.makeText(ConnectionService.this, "online", Toast.LENGTH_SHORT).show();
}
if(!isConnected){
dbRef.child("online").onDisconnect().setValue(ServerValue.TIMESTAMP);
Toast.makeText(ConnectionService.this, "offline", Toast.LENGTH_SHORT).show();
}
}
}
}
应用
public class MyApplication extends Application {
public static MyApplication mInstance;
DatabaseReference dbRef;
@Override
public void onCreate() {
super.onCreate();
mInstance=this;
Common.DEVICE_NAME = android.os.Build.MODEL;
FirebaseApp.initializeApp(this);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
public static synchronized MyApplication getInstance(){
return mInstance;
}
public void setConnectivtyListner(ConnectivityReciever.ConnectivityRecieverListner listner){
ConnectivityReciever.connectivityReceiverListener = listner;
}
}
我已经尝试了所有方法,但没有任何效果。
我们将获得帮助。
谢谢
【问题讨论】:
-
什么不起作用?你有错误吗?
-
当没有互联网连接时,onDisconnet 不会将时间戳值保存到 firebase 数据库
-
这是错误
-
从post查看 Frank van Puffelen 的回答。
-
@AlexMamo 感谢分享。但问题是 onDisconnect() 在互联网连接时没有启动。并且没有实时保存时间戳的值?
标签: android firebase firebase-realtime-database service internet-connection