如何在Fire中使用Firebase EventListener作为后台服务?

Muh*_*dNe 7 service android firebase firebase-realtime-database

我正在尝试创建一个即使应用程序关闭也运行的后台服务.此服务应该监听Firebase更改,并根据触发器启动应用程序.我不知道我是否遗漏了代码中的某些内容,或者甚至没有找到正确的答案,但这里是我的代码:

public class FireBaseService extends Service {

private HashMap<String, String> fireBaseBattery;

@Override
public void onCreate() {
    super.onCreate();

    fireBaseBattery = new HashMap<>();
    final Firebase firebaseRef_Battery = new Firebase("the url i want to take data from");

    firebaseRef_Battery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue() != null) {
                fireBaseBattery = (HashMap<String, String>) dataSnapshot.getValue();
                String battery = (String) fireBaseBattery.get("battery");
                int battery_int = Integer.parseInt(battery);

                System.out.println("SERVICE FIREBASE : " + battery);
                if (battery_int <= 10) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                }
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
Run Code Online (Sandbox Code Playgroud)

}

这是显而易见的:

    <uses-permission android:name="android.permission.READ_SMS" />
    <application
        android:name=".ParseApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

<service android:enabled="true"
        android:name=".FireBaseService">
    </service>
        <activity
            android:name=".Launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

编辑:我在MainActivity.class中添加了一行来启动服务

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, FireBaseService.class));
}
Run Code Online (Sandbox Code Playgroud)

Muh*_*dNe 2

我按如下方式编辑了应用程序:

  1. Service 类扩展了 Activity 而不是 Service
  2. 我直接在应用程序标记内添加了服务清单,如下所示:

android:name=".FireBaseService"

  1. 无需从主要活动中启动服务。

编辑后的清单:

 <uses-permission android:name="android.permission.READ_SMS" />
<application
    android:name=".FireBaseService"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".Launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)