(Android) Hold a Socket.IO Connection throughout Activities - android

I'm basically trying to start a Socket.IO connection in one activity (The LogIn Activity) and carry that throughout the various other activities. If anyone has any idea on what would work the best in this situation that would be great. All I need is probably a service that I can run in the background but I haven't been able to figure out how that works.
I tried doing this but that doesn't work.
public class KlassAttack extends Application {
Socket gameConnection;
#Override
public void onCreate() {
try {gameConnection = IO.socket("http://71.13.36.124:56543");}catch(URISyntaxException e){}
gameConnection.connect();
Log.e("TEST", "EEE");
}
}

U can make a singleton class
public class SocketIOClient {
private static Socket mSocket;
private static void initSocket(Activity activity) {
try {
mSocket = IO.socket(Constants.CHAT_SOCKET_URL);
} catch (URISyntaxException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static Socket getInstance(Activity activity) {
if (mSocket != null) {
return mSocket;
} else {
initSocket(activity);
return mSocket;
}
} }

Related

How to get Socket Id in Socket.io on Android?

I want to get the id of my socket running in Android. Everything is create normally and is able to send data back and forth but I need to get the Androids socket id for usage throughout the entire application. I have tried to find a solution but either they were outdated or does not work. Below is my latest attempt at solving this issue. With this attempt it errors saying that there are no elements in args[0]. I have also tried mSocket.id(); but that returns nothing.
public class GameSocket {
private Socket mSocket = null;
private static GameSocket mInstance;
public synchronized static GameSocket getInstance() {
if (mInstance == null) {
mInstance = new GameSocket();
}
return mInstance;
}
public void initialize() {
mSocket = IO.socket(URI.create(SERVER_URI));
mSocket.off();
mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
#Override
public void call(Object... args) {
Log.d(GameSocket.class.getSimpleName(), "Event connection...");
mSocketId = args[0].toString();
Log.d("CONNECT ID", mSocketId);
}
})
.on(Socket.EVENT_ERROR, new Emitter.Listener() {
#Override
public void call(Object... args) {
if (args != null) {
Log.e(GameSocket.class.getSimpleName(), "Socket Event Error: " + args[0].toString());
}
}
});
}
}

Socket.isConnected() make my android app force close

I don't know what happen with my source code about Socket in Android, when I use method
.isConnected()
My app always force close. And here my source code
public class MyActivity extends Activity {
private String IP;
private int PORT;
private Socket socket;
private PrintWriter printWriter;
private TextView text;
private EditText fieldIp;
private EditText fieldPort;
private Button connect;
private FrameLayout frameIP;
private String message;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
frameIP = (FrameLayout)findViewById(R.id.frameIP);
connect = (Button)findViewById(R.id.connect);
fieldIp = (EditText)findViewById(R.id.ip);
fieldPort = (EditText)findViewById(R.id.port);
text = (TextView)findViewById(R.id.keterangan);
connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IP = fieldIp.getText().toString();
PORT = Integer.parseInt(fieldPort.getText().toString());
SocketConnect socketConnect = new SocketConnect(IP,PORT);
socketConnect.execute();
}
});
}
private class SocketConnect extends AsyncTask<Void, Void, Boolean> {
String ip;
int port;
public SocketConnect(String a, int b){
this.ip = a;
this.port = b;
}
#Override
protected Boolean doInBackground(Void... params) {
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip,port));
if(socket.isConnected())
{
text.setText("Connected!");
}
else
{
text.setText("Failed to connect!");
}
} catch (IOException e) {
Log.e("MyActivity",e.getMessage());
}
finally {
startActivity(new Intent(getApplicationContext(),ListViewText.class));
}
return null;
}
}
}
And I use this in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
I hope you can help me guys :(
Change the doInBackground method as follows...
#Override
protected Boolean doInBackground(Void... params) {
boolean success = true;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port));
} catch (Exception e) {
success = false;
Log.e("MyActivity", e.getMessage());
}
return success;
}
Then add an onPostExecute method...
#Override
protected void onPostExecute(boolean result) {
if(result) {
text.setText("Connected!");
startActivity(new Intent(MyActivity.this, ListViewText.class));
}
else {
text.setText("Failed to connect!");
}
}
First thing you are calling UI operation outside of UI thread (that is why AsyncTask was created, to handle background job only in doInBackground) So problem about displaying text un TextView is solved...
But more important thing:
Never open Socket in AsyncTask. On Android developer site you can find following:
If you need to keep threads running for long periods of time, it is
highly recommended you use the various APIs provided by the
java.util.concurrent package such as Executor, ThreadPoolExecutor and
FutureTask.)
And that is exactly what you want to do. So use Service, Thread or those mentioned above instead.

Hiding DataBaseHelper & DAOs from my Activity

I want to call CRUD operations on Order objects in my Activity. I was wondered is the following implementation of a "Service" class a good way to do this? I don't want any reference to DatabaseHelper or DAO objects in my Activity code as I don't think this would be desireable.
Here is my Service class
public class OrderService
{
private static OrderService instance;
private static Dao<Order, Integer> orderDAO;
static public void init(Context ctx) {
if (null == instance) {
instance = new OrderService(ctx);
}
}
public static OrderService getInstance() {
return instance;
}
private OrderService(Context ctx) {
DatabaseHelper helper = DatabaseHelper.getInstance(ctx);
helper.getWritableDatabase();
orderDAO = helper.getOrderDao();
}
public Order getOrderWithId(int orderId) {
Order myOrder = null;
try {
myOrder = orderDAO.queryForId(orderId);
} catch (SQLException e) {
e.printStackTrace();
}
return myOrder;
}
public Order neworder(Order order) {
try {
orderDAO.create(order);
} catch (SQLException e) {
e.printStackTrace();
}
return order;
}
public void deleteorder(Order order) {
try {
orderDAO.delete(order);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateorder(Order order) {
try {
orderDAO.update(order);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List <Order> getordersForCategory(int orderId) {
List <Order> orders = null;
try {
orders = orderDAO.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return orders;
}
}
and here is how I intend to use the service
public class OrderProcessingActivity extends Activity {
int orderID;
private Order order;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview);
order = OrderService.getInstance().getOrderWithId(orderID);
......
Does this look like a good way to access the SQLlite DB ?
I have read about "Service" implementations that can be configured in Android so I was sondered is this something that I should be using instead?
Despite moving your database logic to a different class, you're doing all of your database operations in the UI thread, which is not ideal. Also note that even though your class is called "service" it doesn't inherit from any of the Service classes in Android.
One alternate approach would be to do your database operations from the doInBackground method of an AsyncTask, return your needed data from that method. Then, use the returned data to update your activity in the onPostExecute method.
This is more or less the approach I take. My application architecture typically looks like this:
Activity <--> Service <--> DAO <--> SQLite
This looks pretty close to what you have, so I'd say it looks good! I normally don't implement it as a singleton, however, as I don't like to keep the same Context around for the entire lifetime of the app. Instead, I pass in the Context to create a service from each Activity.

Can I use ORMlite/SQLlite outside of a containing application

I want to unit test a DatabaseService class I have created ( Modified a WishList application I found online )
package com.test.db;
import java.sql.SQLException;
import java.util.List;
import android.content.Context;
import com.test.model.WishList;
public class WishListService
{
private static WishListService instance;
public static void init(Context ctx)
{
if (null == instance)
{
instance = new WishListService(ctx);
}
}
static public WishListService getInstance()
{
return instance;
}
private DatabaseHelper helper;
private WishListService(Context ctx)
{
helper = DatabaseHelper.getInstance(ctx);
}
public List<WishList> getAllWishLists()
{
List<WishList> wishLists = null;
try
{
wishLists = helper.getWishListDao().queryForAll();
} catch (SQLException e)
{
e.printStackTrace();
}
return wishLists;
}
public void addWishList(WishList l)
{
try
{
helper.getWishListDao().create(l);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public WishList getWishListWithId(int wishListId)
{
WishList wishList = null;
try
{
wishList = helper.getWishListDao().queryForId(wishListId);
} catch (SQLException e)
{
e.printStackTrace();
}
return wishList;
}
public void deleteWishList(WishList wishList)
{
try
{
helper.getWishListDao().delete(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public void updateWishList(WishList wishList)
{
try
{
helper.getWishListDao().update(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
My question is is there any way to instantiate the DatabaseHelper without having to create a test activity and pass it in as the Context that DatabaseHelper requires?
Ideally I want to Unit test this class as a standard JUnit test , not an Android JUnit test
I'm not sure this is a good idea. ORMLite has non-Android JDBC package that you could use to drive Sqlite directly via JDBC. However if the goal is to test your Android database classes, you will be running a lot of non-android code which would, in my view, invalidate the tests. I'm considered mocking out all of the Android classes but it starts to become a maze of twisting passages before long.
I think your best way to proceed would be to put up with the Android junit tests. Take a look at the ORMLite Android test package files for examples. The [BaseDaoTest file] has all of the setup/shutdown methods that you can customize for your own tests.

Timing issue in an Android JUnit test when observing an object modifed by another thread

My Android app should do the following:
The MainActivity launches another thread at the beginning called UdpListener which can receive UDP calls from a remote server. If it receives a packet with a content "UPDATE", the UdpListener should notify the MainActivity to do something.
(In the real app, the use case looks like this that my app listens on the remote server. If there is any new data available on the remote server, it notifies every client (app) by UDP, so the client knows that it can download the new data by using HTTP).
I tried to simulate this in an JUnit test. The test contains an inner class which mocks the MainActivity as well as it sends the UDP call to the UdpListener:
public class UdpListener extends Thread implements Subject {
private DatagramSocket serverSocket;
private DatagramPacket receivedPacket;
private boolean running = false;
private String sentence = "";
private Observer observer;
private static final String TAG = "UdpListener";
public UdpListener(Observer o) throws SocketException {
serverSocket = new DatagramSocket(9800);
setRunning(true);
observer = o;
}
#Override
public void run() {
setName(TAG);
while (isRunning()) {
byte[] receivedData = new byte[1024];
receivedPacket = new DatagramPacket(receivedData, receivedData.length);
try {
serverSocket.receive(receivedPacket);
}
catch (IOException e) {
Log.w(TAG, e.getMessage());
}
try {
sentence = new String(receivedPacket.getData(), 0, receivedPacket.getLength(), "UTF-8");
if ("UPDATE".equals(sentence)) {
notifyObserver();
}
}
catch (UnsupportedEncodingException e) {
Log.w(TAG, e.getMessage());
}
}
}
private boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
#Override
public void notifyObserver() {
observer.update();
}
}
This is the corresponding test:
#RunWith(RobolectricTestRunner.class)
public class UdpListenerTest {
private MainActivityMock mainActivityMock = new MainActivityMock();
#Before
public void setUp() throws Exception {
mainActivityMock.setUpdate(false);
}
#After
public void tearDown() throws Exception {
mainActivityMock.setUpdate(false);
}
#Test
public void canNotifyObserver() throws IOException, InterruptedException {
UdpListener udpListener = new UdpListener(mainActivityMock);
udpListener.setRunning(true);
udpListener.start();
InetAddress ipAddress = InetAddress.getByName("localhost");
DatagramSocket datagramSocket = new DatagramSocket();
DatagramPacket sendPacket = new DatagramPacket("UPDATE".getBytes(), "UPDATE".length(), ipAddress, 9800);
datagramSocket.send(sendPacket);
datagramSocket.close();
assertTrue(mainActivityMock.isUpdate());
udpListener.setRunning(false);
}
private class MainActivityMock implements Observer {
private boolean update = false;
#Override
public void update() {
update = true;
}
public boolean isUpdate() {
return update;
}
public void setUpdate(boolean update) {
this.update = update;
}
}
}
The good thing is that my concept works. But, this test doesn't. That means it only does when I stop with a breakpoint at this line datagramSocket.close(); and wait for about a second. Why this happens is clear. But how can I do that automatically? I thought about using wait() but I have to invoke notify() from the other thread for that. The same problem with CountDownLatch. I'm not sure how to solve that without changing UdpListener.
You could write a simple loop with a specified timeout.
try {
long timeout = 500; // ms
long lastTime = System.currentTimeMillis();
while(timeout > 0 && !mainActivityMock.isUpdate()) {
Thread.sleep(timeout);
timeout -= System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
}
} catch(InterruptedException e) {
} finally {
assertTrue(mainActivityMock.isUpdate());
}
By the way - you should declare your running attribute to volatile.
one solution would be to use a blocking queue with size 1 for storing your received results.
The request for isUpdate (which would take an element from the blocking queue) would block until the update package(or any other package) is put into the queue.
is case you want all your calls to be non-blocking you could use a Future for receiving your result. Future.get() would block ontil your result is received.

Categories

Resources