Search code examples
androidbroadcastreceiverandroid-wake-lock

Keep Internet Connection during sleep - Softphone application


I am trying to create a softphone application that can receive and make calls with a users credentials. I have everything working but the issue I am having now is keeping the user logged in, or registered, to the SIP Client. I have read many articles to keep the Wifi connection alive while I am trying to connect. I added some code I got from another program and even that doesn't work.

What I really need is a way for the phone to stay registered all the time. I know it can be done because of apps like Linphone and Bria. Does anyone have any suggestions.

Here is my onNetworkChangeReceiver which listens for network changes. Please let me know if this code is garbage and if you have any suggestions please let me know

import com.zxd.activity.util.PhoneState;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class OnNetworkChangeReceiver extends WakefulBroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      PhoneState phoneState = PhoneState.getInstance();
      // This is the Intent to deliver to our service.
      Intent service = new Intent(context, OnNetworkChangeListener.class);
      if(phoneState.threadIntent != null){
          WakefulBroadcastReceiver.completeWakefulIntent(phoneState.threadIntent);
      }
      phoneState.setThreadIntent(intent);
      // Start the service, keeping the device awake while it is launching.
      startWakefulService(context, service);
  }
}

PS: This receiver gets android.net.conn.CONNECTIVITY_CHANGE

This is the OnNetworkChangeListener

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

import com.zxd.activity.SipManager;

public class OnNetworkChangeListener extends IntentService{
private static String TAG = "OnNetworkChangeListener";
private static Intent i;
public OnNetworkChangeListener() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    i = intent;
    SipManager sipManager = SipManager.getInstance();
    sipManager.onNetworkChange();
}

@Override
public void onDestroy(){
    super.onDestroy();
    if(i != null){
        WakefulBroadcastReceiver.completeWakefulIntent(i);
    }
}

}

Solution

  • For my solution I looked at linphone. They are an open-source softphone application and they have a great way to implement a KeepAlive Service and KeepAlive Handler. This has worked for me