Skip to content

Commit

Permalink
add isRunning method to see if Lantern is already running in the back…
Browse files Browse the repository at this point in the history
…ground
  • Loading branch information
Todd Blose committed Mar 30, 2016
1 parent 383b88d commit af55015
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ protected void onCreate(Bundle savedInstanceState) {
// since onCreate is only called when the main activity
// is first created, we clear shared preferences in case
// Lantern was forcibly stopped during a previous run
LanternUI.clearPreferences();
if (!Service.isRunning(context)) {
LanternUI.clearPreferences();
}

// the ACTION_SHUTDOWN intent is broadcast when the phone is
// about to be shutdown. We register a receiver to make sure we
Expand Down Expand Up @@ -141,7 +143,6 @@ protected void onDestroy() {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
stopLantern();
} catch (Exception e) {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.getlantern.lantern.vpn;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

Expand Down Expand Up @@ -27,6 +30,18 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

// isRunning checks to see if the VPN service is already running
// in the background.
public static boolean isRunning(Context c) {
ActivityManager manager = (ActivityManager)c.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (Service.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

@Override
public synchronized void run() {
try {
Expand Down

0 comments on commit af55015

Please sign in to comment.