Reputation: 95
I am developing an XMPP chat client.I am using Background service to connect to network XMPP server.however when i start the server it gives Network on UI thread Exception. when i downgrade the Android SDK to 8 it gives ANR exception
I tried starting the Service from onCreate method of the SPlash screen.
Runnable runnable = new Runnable() {
@Override
public void run() {
Intent serviceIntent=new Intent();
serviceIntent.setClass(getApplicationContext(), SeetiService.class);
startService(serviceIntent);
}
};
new Thread(runnable).start();
Thread serviceThread=new Thread()
{
public void run()
{
Intent serviceIntent=new Intent();
serviceIntent.setClass(getApplicationContext(), SeetiService.class);
startService(serviceIntent);
}
};
serviceThread.start();
But i still get the Same network on Main UI thread.
Can some one help?
Thanks
Upvotes: 0
Views: 1533
Reputation: 28103
It seems that you are trying to start two Service simultaneously.AFAIK You can not start it with above piece of code.But i will show why you are facing ANR.
Intent serviceIntent=new Intent();
serviceIntent.setClass(getApplicationContext(), SeetiService.class);
startService(serviceIntent);
The above code is supposed to run on UI thread.and if you want to run it from another thread you must embedd it into runOnUiThread.So your block should look like.
Runnable runnable = new Runnable()
{@Override
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
Intent serviceIntent = new Intent();
serviceIntent.setClass(getApplicationContext(), SeetiService.class);
startService(serviceIntent);
}
});
}
};
new Thread(runnable).start();
Upvotes: 1