Android RuntimeException: Unable to instantiate the service

In your concrete implementation you have to declare a default constructor which calls the public IntentService (String name) super constructor of the abstract IntentService class you extend: public MyService () { super(“MyServerOrWhatever”); } You do not need to overwrite onStartCommand if the super implementation fits for you (what I expect). In your current case you … Read more

Minimal android foreground service killed on high-end phone

A service started by startForeground belongs to the second most important group visible process: A visible process is doing work that the user is currently aware of, so killing it would have a noticeable negative impact on the user experience. A process is considered visible in the following conditions: It is running an Activity that … Read more

Check if Activity is running from Service

Use the below method with your package name. It will return true if any of your activities is in foreground. public boolean isForeground(String myPackage) { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); ComponentName componentInfo = runningTaskInfo.get(0).topActivity; return componentInfo.getPackageName().equals(myPackage); } Update: Add Permission: <uses-permission android:name=”android.permission.GET_TASKS” />

START_STICKY does not work on Android KitKat

Seems that this is a bug present in Android 4.4, got around it with the following: @Override public void onTaskRemoved(Intent rootIntent) { Intent restartService = new Intent(getApplicationContext(), this.getClass()); restartService.setPackage(getPackageName()); PendingIntent restartServicePI = PendingIntent.getService( getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI); } Found this answer from this post

Can I get data from shared preferences inside a service?

You can access the default shared preferences instance, which is shared across all your Activity and Service classes, by calling PreferenceManager.getDefaultSharedPreferences(Context context): SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); This is great for storing simple primitives (like booleans) or serializable objects. However, if you’re capturing a lot of location data, you might consider using a SQLite database instead.

Continue Service even if application is cleared from Recent app

Just follow these scenarios, your service and processes (Threads run inside your service) will remain continuous. Create service and use START_STICKY as return value in onStartCommand method like below: @Override public int onStartCommand(final Intent intent, final int flags, final int startId) { //your code return START_STICKY; } Above code will Restart the service if destroyed … Read more

Why does my Android service get restarted when the process is killed, even though I used START_NOT_STICKY?

Look to this document section: Service lifecycle changes (since 1.6) updated after some investigations (have created project, run described command step by step, etc) i found that code is working exactly as described in “Service lifecycle changes” what I found: adb shell am kill com.tavianator.servicerestart kills nothing (try adb shell , then in shell am … Read more

How to execute Async task repeatedly after fixed time intervals

public void callAsynchronousTask() { final Handler handler = new Handler(); Timer timer = new Timer(); TimerTask doAsynchronousTask = new TimerTask() { @Override public void run() { handler.post(new Runnable() { public void run() { try { PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask(); // PerformBackgroundTask this class is the class that extends AsynchTask performBackgroundTask.execute(); } catch (Exception e) … Read more