how to prevent service to run again if already running android

A service will only run once, so you can call startService(Intent) multiple times. You will receive an onStartCommand() in the service. So keep that in mind. Source: Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started … Read more

Android O Replacement for getRunningServices

It is intentional that there is no replacement for this function. It is not too clear from the documentation, but from the docs: Note: this method is only intended for debugging or implementing service management type user interfaces. Basically, using the method to check other apps services was an unintended side-effect, so Google decided to … Read more

Bad notification posted – Couldn’t expand RemoteViews for: StatusBarNotification

For me, the problem was that I was setting a specific height for the root layout, in the custom notification view xml file. As soon as I changed: android:layout_height=”@dimen/notification_expanded” to android:layout_height=”match_parent” in the root layout of notification view, the problem was solved. Also take a look at this example to see a simple example of … Read more

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

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

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

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