startForeground fail after upgrade to Android 8.1

After some tinkering for a while with different solutions i found out that one must create a notification channel in Android 8.1 and above. private fun startForeground() { val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(“my_service”, “My Background Service”) } else { // If earlier version channel ID is not used // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) “” … Read more

Calling activity class method from Service class

Define an interface your Service will use to communicate events: public interface ServiceCallbacks { void doSomething(); } Write your Service class. Your Activity will bind to this service, so follow the sample shown here. In addition, we will add a method to set the ServiceCallbacks. public class MyService extends Service { // Binder given to … Read more

Android: keeping a background service alive (preventing process death)

For Android 2.0 or later you can use the startForeground() method to start your Service in the foreground. The documentation says the following: A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and … Read more

JavaFX2: Can I pause a background Task / Service?

Solution When your background process encounters a situation where it requires a user to be prompted for input, use FutureTask executed in Platform.runLater to showAndWait the dialog prompt on the JavaFX application thread. In the background process use futureTask.get to pause the background process until the user has input the necessary values which will allow … Read more

How to display a Dialog from a Service?

We can show dialog from service only if it is a system alert dialog. So, set TYPE_SYSTEM_ALERT window layout parameter to Dialog as follows: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); But, it needs SYSTEM_ALERT_WINDOW permission. So, don’t forget to add this permissin in Manifest file. <uses-permission android:name=”android.permission.SYSTEM_ALERT_WINDOW”/> Edit: Better option to show dialog is to start activity as one of … Read more

Using Alarmmanager to start a service at specific time

HI friends, After a lot of researching and with reference from “Pentium10″‘s question on the same topic i managed to get it working. Though i still cant understand why the “date” concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly. Calendar cur_cal = new GregorianCalendar(); cur_cal.setTimeInMillis(System.currentTimeMillis());//set the … Read more

Run a Java Application as a Service on Linux

I wrote another simple wrapper here: #!/bin/sh SERVICE_NAME=MyService PATH_TO_JAR=/usr/local/MyProject/MyJar.jar PID_PATH_NAME=/tmp/MyService-pid case $1 in start) echo “Starting $SERVICE_NAME …” if [ ! -f $PID_PATH_NAME ]; then nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME echo “$SERVICE_NAME started …” else echo “$SERVICE_NAME is already running …” fi ;; stop) if [ … Read more

How to check if Location Services are enabled?

You can use the below code to check whether gps provider and network providers are enabled or not. LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean network_enabled = false; try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) {} try { network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) {} if(!gps_enabled && !network_enabled) { // notify user … Read more