Controlling Session Lifetime: VoIP Application

If your application uses VoIP and goes to the background and you wish to prevent the session from ending there are a few things you have to do. Your application needs to have an event when there is an active call, and when the call has ended.

  1. Call Appsee.start() in every Activity.onCreate() (regular integration)
  2. Use Appsee.addAppseeListener() to register on our callbacks. Make sure to register only once!
  3. As we want to prevent the session from ending if we're in a call, each time AppseeListener.onAppseeSessionEnding() is getting called the app should use: sessionEndingInfo.setShouldEndSession(CallManager.isCallInProgress());
  4. When a call is ended externally (from the notification center for example) we should end the session, therefore - when a call is ended, we need to call Appsee.finishSession(true, true); If the call is ended when the app is still in foreground, Appsee will detect it and do nothing, so you can call this method in every call end.

 

Example

package com.basicme;

import com.appsee.Appsee;
import com.appsee.AppseeListener;
import com.appsee.AppseeScreenDetectedInfo;
import com.appsee.AppseeSessionEndedInfo;
import com.appsee.AppseeSessionEndingInfo;
import com.appsee.AppseeSessionStartedInfo;
import com.appsee.AppseeSessionStartingInfo;
import android.app.Activity;
import android.net.sip.SipAudioCall;
import android.net.sip.SipProfile;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class VoipActivity extends Activity {
 private static AppseeListener listener = null;
 private boolean isCallActive = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_voip);

  SipAudioCall.Listener callListener = new SipAudioCall.Listener() {
   @Override
   public void onRinging(SipAudioCall call, SipProfile caller) {
    super.onRinging(call, caller);
   }

   @Override
   public void onCallEstablished(SipAudioCall call) {
    call.startAudio();
    call.setSpeakerMode(true);
    call.toggleMute();
    isCallActive = true;
   }

   @Override
   public void onCallEnded(SipAudioCall call) {
    isCallActive = false;
    Appsee.finishSession(true, true);
   }
  };

  if (listener != null) {
   listener = new AppseeListener() {
    @Override
    public void onAppseeSessionStarting(AppseeSessionStartingInfo startingInfo) {
     // TODO Auto-generated method stub
    }

    @Override
    public void onAppseeSessionStarted(AppseeSessionStartedInfo sessionInfo) {
     // TODO Auto-generated method stub
    }

    @Override
    public void onAppseeSessionEnding(AppseeSessionEndingInfo sessionEndingInfo) {
     sessionEndingInfo.setShouldEndSession(!isCallActive);
    }

    @Override
    public void onAppseeSessionEnded(AppseeSessionEndedInfo sessionInfo) {
     // TODO Auto-generated method stub
    }

    @Override
    public void onAppseeScreenDetected(AppseeScreenDetectedInfo screenInfo) {
     // TODO Auto-generated method stub
    }
   };

   Appsee.addAppseeListener(listener);
  }

  Appsee.start("MY API KEY");
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.voip, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

Did you find this article helpful?