2017-12-31 42 views
-3

내 앱을 최소화하고 해당 활동을 실행하면 작동하지만 다른 활동에서 해당 활동으로 갔을 때 작동하지 않습니다. 프로젝트에 활동이 있지만 활동이 시작될 때 firebase에 노드가 있지만 온라인 상태 여야하지만 작동하지 않습니다.

auth = FirebaseAuth.getInstance(); 
     currentUser = auth.getCurrentUser(); 

     db = FirebaseDatabase.getInstance(); 
     userRef = db.getReference().child("Users").child(auth.getCurrentUser().getUid()); 

     if(currentUser == null) { 
      sendToStart(); 
     } else { 
      userRef.child("online").setValue("true"); 
     } 

아래 내 ONSTART 방법 같은 것을 가지고있는 것처럼 내에서 onCreate도 있지만 작업에서

.
도와주세요.

답변

0

백그라운드 서비스에서이 명령문을 실행하여 노드를 업데이트해야합니다.

public class MyService extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     //TODO do something useful 

    auth = FirebaseAuth.getInstance(); 
    currentUser = auth.getCurrentUser(); 

    db = FirebaseDatabase.getInstance(); 
    userRef = db.getReference().child("Users").child(auth.getCurrentUser().getUid()); 

    if(currentUser == null) { 
     sendToStart(); 
    } else { 
     userRef.child("online").setValue("true"); 
    } 


     return Service.START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
    //TODO for communication return IBinder implementation 
    return null; 
    } 
} 


Intent i= new Intent(context, MyService.class); 
context.startService(i); 
+0

어떻게 할 수 있습니까? – Hemal

+0

체크 답변을 추가했습니다. –

관련 문제