无法在远程(与主线程分离)进程上获取DatabaseReference和FIrebaseAuth currentUser

时间:2016-09-22 14:49:05

标签: android firebase firebase-realtime-database firebase-authentication

我想创建一个单独的进程,该进程将使用单独的进程接收和更新Firebase数据库,以便即使应用程序(主线程)被停止或终止也会继续运行。但是它会在下面返回我的例外情况。

错误

09-22 22:39:02.229 1669-1669/com.tradesomev4.tradesomev4:remote E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.tradesomev4.tradesomev4:remote, PID: 1669
                                                                              java.lang.RuntimeException: Unable to create service com.tradesomev4.tradesomev4.m_Helpers.LocationService: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3005)
                                                                                  at android.app.ActivityThread.access$1800(ActivityThread.java:178)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                  at android.os.Looper.loop(Looper.java:194)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                                               Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                  at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                  at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                  at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                                  at com.tradesomev4.tradesomev4.m_Helpers.LocationService.onCreate(LocationService.java:128)
                                                                                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2995)
                                                                                  at android.app.ActivityThread.access$1800(ActivityThread.java:178) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1553) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                  at android.os.Looper.loop(Looper.java:194) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

清单

    <service
        android:name=".m_Helpers.LocationService"
        android:enabled="true"
        android:exported="true"
        android:process=":remote">

    </service>

服务Java

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(DEBUG_TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

 @Override
public void onCreate() {
    Log.e(DEBUG_TAG, "onCreate");

        databaseReference = FirebaseDatabase.getInstance().getReference("https://project-7989634332026765831.firebaseio.com/");
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
}

希望得到您的回复:)

1 个答案:

答案 0 :(得分:0)

将此代码apply plugin: 'com.google.gms.google-services'移动到build.gradle文件的开头