project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetplayService.java
changeset 7349 12fdfd2038d4
parent 7346 b0f67c5b4215
child 7355 5673e95ef647
equal deleted inserted replaced
7346:b0f67c5b4215 7349:12fdfd2038d4
     5 import android.app.Service;
     5 import android.app.Service;
     6 import android.content.Intent;
     6 import android.content.Intent;
     7 import android.os.Binder;
     7 import android.os.Binder;
     8 import android.os.CountDownTimer;
     8 import android.os.CountDownTimer;
     9 import android.os.IBinder;
     9 import android.os.IBinder;
       
    10 import android.util.Log;
    10 
    11 
    11 public class NetplayService extends Service {
    12 public class NetplayService extends Service {
    12 	private final NetplayBinder binder = new NetplayBinder();
    13 	private final NetplayBinder binder = new NetplayBinder();
    13 	public Netconn netconn;
    14 	public Netconn netconn;
    14 	private CountDownTimer timer;
    15 	private CountDownTimer timer;
    18 		return binder;
    19 		return binder;
    19 	}
    20 	}
    20 	
    21 	
    21 	@Override
    22 	@Override
    22 	public void onCreate() {
    23 	public void onCreate() {
       
    24 		Log.d("NetplayService", "Creating");
    23 		if(Flib.INSTANCE.flib_init() != 0) {
    25 		if(Flib.INSTANCE.flib_init() != 0) {
    24 			throw new RuntimeException("Unable to start frontlib");
    26 			throw new RuntimeException("Unable to start frontlib");
    25 		}
    27 		}
    26 		try {
    28 		try {
    27 			netconn = new Netconn(getApplicationContext(), "AndroidTester");
    29 			netconn = new Netconn(getApplicationContext(), "AndroidTester");
    30 			throw new RuntimeException("Unable to start frontlib", e);
    32 			throw new RuntimeException("Unable to start frontlib", e);
    31 		}
    33 		}
    32     	timer = new CountDownTimer(Long.MAX_VALUE, 50) {
    34     	timer = new CountDownTimer(Long.MAX_VALUE, 50) {
    33 			@Override
    35 			@Override
    34 			public void onTick(long millisUntilFinished) {
    36 			public void onTick(long millisUntilFinished) {
    35 				if(netconn != null) {
    37 				if(netconn != null && netconn.isConnected()) {
    36 					netconn.tick();
    38 					netconn.tick();
    37 				}
    39 				}
    38 			}
    40 			}
    39 			
    41 			
    40 			@Override
    42 			@Override
    44 		timer.start();
    46 		timer.start();
    45 	}
    47 	}
    46 	
    48 	
    47 	@Override
    49 	@Override
    48 	public void onDestroy() {
    50 	public void onDestroy() {
       
    51 		Log.d("NetplayService", "Destroying");
    49 		timer.cancel();
    52 		timer.cancel();
    50 		netconn.disconnect();
    53 		netconn.disconnect();
    51 		Flib.INSTANCE.flib_quit();
    54 		Flib.INSTANCE.flib_quit();
    52 	}
    55 	}
    53 
    56