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));
}
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.
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.
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.
I edited the Application as following :
android:name=".FireBaseService"
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With