Я уже задавал вопрос, где мое приложение работало только на эмуляторе, а не на реальном устройстве, вот ссылка на него:
База данных Firebase не обновляется без ошибок, медленный ответ
к сожалению, я не смог выяснить проблему или решить ее. Итак, я начал с нуля и создал простое приложение, которое записывает «TEST» в базу данных в реальном времени, и та же проблема была в том, что приложение работает ТОЛЬКО на эмуляторе, но не на моем реальном устройстве.
Итак, я выяснил, что приложение не может подключиться к Firebase, когда я запустил его на своем устройстве. Но я не мог понять, почему?
Вот мое основное занятие:
package com.example.alice.testingfirebase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference();
for (int i = 0; i<10;i++) {
String id = i+"";
mDatabase.child("users").child(id).setValue("TEST");
}
}
}
Вот мой манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alice.testingfirebase">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
и вот мой build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.alice.testingfirebase"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
Правила базы данных установлены как общедоступные.
После добавления разрешения ИНТЕРНЕТ приложение продолжает останавливаться, и вот часть журнала cat:
09-09 14:39:55.374 21502-21537/com.example.alice.testingfirebase V/FA: Connection attempt already in progress
09-09 14:39:55.455 21502-21502/com.example.alice.testingfirebase D/AndroidRuntime: Shutting down VM
09-09 14:39:55.456 21502-21502/com.example.alice.testingfirebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alice.testingfirebase, PID: 21502
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alice.testingfirebase/com.example.alice.testingfirebase.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setLogLevel() must be made before any other usage of FirebaseDatabase instance.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: com.google.firebase.database.DatabaseException: Calls to setLogLevel() must be made before any other usage of FirebaseDatabase instance.
at com.google.firebase.database.FirebaseDatabase.zzb(Unknown Source:38)
at com.google.firebase.database.FirebaseDatabase.setLogLevel(Unknown Source:3)
at com.example.alice.testingfirebase.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
09-09 14:39:55.472 21502-21551/com.example.alice.testingfirebase W/zygote64: Skipping duplicate class check due to unrecognized classloader
09-09 14:39:55.474 21502-21551/com.example.alice.testingfirebase I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
09-09 14:39:55.475 21502-21551/com.example.alice.testingfirebase I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
09-09 14:39:55.506 21502-21551/com.example.alice.testingfirebase W/zygote64: Skipping duplicate class check due to unrecognized classloader
09-09 14:39:55.567 21502-21559/com.example.alice.testingfirebase D/NetworkSecurityConfig: No Network Security Config specified, using platform default
09-09 14:39:55.573 21502-21559/com.example.alice.testingfirebase D/TcpOptimizer: TcpOptimizer-ON
09-09 14:40:00.281 21502-21521/com.example.alice.testingfirebase I/zygote64: WaitForGcToComplete blocked for 22.520ms for cause HeapTrim
09-09 14:40:00.286 21502-21521/com.example.alice.testingfirebase W/zygote64: Suspending all threads took: 5.439ms