Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
Example Activity:
import android.app.Activity;
import android.os.Bundle;

public class TestActivity extends Activity {
private SpringMobileHelper springHelper;

@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 
springHelper = SpringMobileHelper.getInstance(getApplicationContext()); }

@Override
protected void onStart() { super.onStart(); springHelper.start(); }

@Override
protected void onResume() { super.onResume(); springHelper.resume(); }

@Override
protected void onPause() { super.onPause(); springHelper.pause(); }

@Override
protected void onDestroy() { super.onDestroy(); springHelper.destroy(); }

}
Helper Class:
import java.lang.Thread.State;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import de.spring.mobile.SpringMobile;

/**

    This class will help to manage more than one Activity. The Class holds the SpringMobile object and managed the events to it.
    */
    public class SpringMobileHelper {
    private static SpringMobileHelper singelton = null;

private SpringMobile spring = null;

private boolean isStarted = false;
private int foregroundCounter = 0;
private final Thread sendBackground = new Thread() {
@Override
public void run() {
super.run();

while(true) {
synchronized(sendBackground) {
try { wait(); } catch (InterruptedException e) { continue; }

try { sleep(1000); Map<String, Object> map = new HashMap<String, Object>(); 
map.put(SpringMobile.VAR_ACTION, SpringMobile.APP_BACKGROUND); spring.commit(map); } catch (InterruptedException e) {}

}
}
}
};
{sendBackground.start();}

private SpringMobileHelper() {
}

/**
*

    @param the context
    @return SpringMobileHelper
    */
    public static SpringMobileHelper getInstance(Context context)
    Unknown macro: { if(singelton == null) { singelton = new SpringMobileHelper();
   singelton singelton.spring = new SpringMobile("app", "MoreActivities", context); } return singelton; }

/**

    call by start of activity
    */
    public void start()
    Unknown macro: { if(!isStarted) { isStarted = true; Map<String, Object> map = new HashMap<String, Object>(); spring.commit(map); } }

/**

    call by resume of activity
    */
    public void resume()
    Unknown macro: { foregroundCounter++; if(sendBackground != null && sendBackground.getState() != State.WAITING) { sendBackground.interrupt();
    } else { Map<String, Object> map = new HashMap<String, Object>(); map.put(SpringMobile.VAR_ACTION, SpringMobile.APP_FOREGROUND); spring.commit(map); }
    }

    /**
    * call by pause of activity
    */
    public void pause() {
    foregroundCounter--;
    if(sendBackground != null && sendBackground.getState() != State.WAITING) { sendBackground.interrupt(); } }

    synchronized(sendBackground) { sendBackground.notify(); }
    }

/**

    call by destroy of activity
    */
    public void destroy() {
    if(foregroundCounter == 0) {
    try { sendBackground.join(1000); Map<String, Object> map = new HashMap<String, Object>();
   map map.put(SpringMobile.VAR_ACTION, SpringMobile.APP_CLOSED); spring.commit(map); } catch (InterruptedException e) {}
    }
    }

}

...