Skip to content

Commit

Permalink
Prevent service start on every activity creating
Browse files Browse the repository at this point in the history
  • Loading branch information
vladholubiev committed Feb 9, 2015
1 parent a31de14 commit 6379d1b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/ua/samosfator/moduleok/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ua.samosfator.moduleok;

import android.app.ActivityManager;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -77,4 +79,14 @@ public static String getFormattedUpdateTime() {
public static void setUpdateTime(Date updateTime) {
App.updateTime = updateTime;
}

public static boolean isServiceRunning(Class<? extends Service> serviceClass) {
ActivityManager manager = (ActivityManager) App.getContext().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/ua/samosfator/moduleok/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import ua.samosfator.moduleok.event.LogoutEvent;
import ua.samosfator.moduleok.event.RefreshEndEvent;
import ua.samosfator.moduleok.event.RefreshEvent;
import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment;
import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment;
import ua.samosfator.moduleok.fragment.navigation_drawer_fragment.NavigationDrawerFragment;
import ua.samosfator.moduleok.notification.ScoreCheckerService;

Expand All @@ -44,7 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
initToolbar();
initNavigationDrawer();

if (Auth.isLoggedIn() && App.hasInternetConnection()) {
if (Auth.isLoggedIn() && App.hasInternetConnection() && !App.isServiceRunning(ScoreCheckerService.class)) {
startService(new Intent(this, ScoreCheckerService.class));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static int getCount() {
private static void initModulesDatesList() {
modulesDates.clear();
List<Subject> subjects = getCurrentSemesterSubjects();
System.setProperty("java8.util.Spliterators.assume.oracle.collections.impl", "false");
StreamSupport.stream(subjects)
.map(Subject::getModules)
.map(StreamSupport::stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public IBinder onBind(Intent intent) {
@Override
public void onCreate() {
App.registerClassForEventBus(this);
System.setProperty("java8.util.Spliterators.assume.oracle.collections.impl", "false");
Toast.makeText(this, "Congrats! ScoreCheckerService Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
Expand Down

0 comments on commit 6379d1b

Please sign in to comment.