setSocket connection from BroadcastReceiver
Hi I'm using socket.io android client.I wrote code and socket connection is perfect.I have one problem.I want to get connection from broadcast (when devise has start).This is my source
`public class AutoRunService extends BroadcastReceiver { private static Socket socket;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(UApplication.getInstance(), "Application is ready to open ", Toast.LENGTH_SHORT).show();
seSocketConnection(context);
}
}
public void seSocketConnection(final Context context) {
try {
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
socket = IO.socket("http://192.168.101.139:8080",opts);
socket.on("onconnect", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject obj = new JSONObject();
Log.e("AutoRunService", "onconnect");
try {
obj.put("host", "***********");
obj.put("entity", new DeviceManager(UApplication.getInstance()).getDeviceId());
socket.emit("initialize", obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).on("onerror", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("AutoRunService", "onerror");
}
}
).on("device", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("AutoRunService", "device");
Intent i = new Intent(context, WelcomeImageActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("AutoRunService", "EVENT_CONNECT_ERROR");
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
// socket.disconnect();
Log.e("AutoRunService", "EVENT_DISCONNECT");
}
});
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
`}
<receiver
android:name=".AutoRunService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>`
I tested my app and socket connection is okey first time from Broadcast and after some second later socket disconnection and socket.io server stoped(node.js server) I don;t know what is a wrong.if anyone knows solution please help me
p.s my socket connection working perfect in Activities .I have problem when i use BroadcastReceiver`
1 - Your event name seems incorrect, try using socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {..} instead.
2 - Maybe your app is being killed by your android os? I think it's the same issue with #288
- Thanks your attention .onconnect is custom is custom method witch trigger socket connection. Also i tried your version ,but same problem...
- can you tell me solution ? how i can detect if killed or not android os?
@wedneyyuri
I have same the issue, does anyone solved this yet ?