Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Firebase EventListener as a background service in android?

I am trying to make a background service that runs even if the application is closed. this services is supposed to listen to Firebase changes, and start the application according to a trigger. I don't know whether I am missing something in the code or not even close to the right answer, but here is my code :

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;
}

}

And this is the manifest :

    <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>

Edit : I added a line in MainActivity.class to start the service

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, FireBaseService.class));
}
like image 200
MuhammadNe Avatar asked Jun 10 '16 15:06

MuhammadNe


People also ask

How I connect Firebase database in Android application?

Open the Firebase Assistant: Tools > Firebase. In the Assistant pane, choose a Firebase product to add to your app. Expand its section, then click the tutorial link (for example, Analytics > Log an Analytics event). Click Connect to Firebase to connect your Android project with Firebase.

How does Firebase listener work?

Firebase data is written to a FirebaseDatabase reference and retrieved by attaching an asynchronous listener to the reference. The listener is triggered once for the initial state of the data and again anytime the data changes.

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.


1 Answers

I edited the Application as following :

  1. The Service class extends Activity and not Service
  2. I added the service manifest inside the application tag directly as the following :

android:name=".FireBaseService"

  1. There is no need to start the service from main activity.

Edited Manifest:

 <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>

like image 180
MuhammadNe Avatar answered Oct 11 '22 07:10

MuhammadNe