Wednesday, 21 August 2013

Multicast after service starts after boot in Android

Multicast after service starts after boot in Android

I am using Android 4.2 on an OMAP4460 processor from TI. My goal is this,
start a service right after boot that listens for SSDP queries on a
multicast socket. I can do this but when I go to create the
MulticastSocket the I get an exception. The exception is coming from the
underlying datagramSocket. Here is some code snippets:
<receiver
android:name="com.example.reciever.StartServicesAtBootReceiver"
android:enabled="true" android:exported="false"
android:label="StartServicesAtBootRemoteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
my onRecieve() looks like this:
public void onReceive(Context context, Intent intent) {
Intent ServiceIntent = new Intent(context,
myService.class);
ComponentName serviceStarted = context
.startService(ServiceIntent);
if (serviceStarted != null)
}
And finally, the service snippet:
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if (null == ssdpServer) {
ssdpServer = new UDPListenThread();
ssdpServer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
public class UDPListenThread extends Thread {
protected MulticastSocket socket = null;
public UDPListenThread() throws IOException {
this("UDPListenThread");
}
public UDPListenThread(String name) throws IOException {
super(name);
}
@Override
public void run() {
Socket mySocket = new Socket();
socket = new MulticastSocket(SSDP_PORT);
// socket.setLoopbackMode(false);
socket.joinGroup(InetAddress.getByName(SSDP_ADDRESS));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
etc.
Note the Socket get allocated ok (its not used here so not sure if it is
valid or not). Also,if I try:
ConnectivityManager cm = (ConnectivityManager) getBaseContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();
from the service or the reciever. activeNetwork comes back as null. Even
if I wait for activeNetwork (from the service) like this:
NetworkInfo activeNetwork = null;
while (null == activeNetwork) {
ConnectivityManager cm = (ConnectivityManager) getBaseContext()
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();
Log.d(this.getClass().getSimpleName(), "activeNetwork "
+ activeNetwork);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
it will remain null even after I am able to pull up the browser and search
from the UI.

No comments:

Post a Comment