Android- Connect to server in Thread - android

When I want to connect to server I got ANR message , some solution is to use Thread concept . The following is my code and the app show force close message. Is there something missing in my code
public void theardupload()
{
new Thread() {
public void run() {
ConnectToServer(url);
}
}.start();
}

Look at Lalit's answer in Android thread not working
Also you must make sure that all exceptions are handled in your ConnectToServer function otherwise the thread will cause an unhandled exception and force close your app

Related

onPongReceived is never called

My team is building an Android application that will use websockets to communicate with an existing backend. We chose to use the AndroidAsync by Koushik Dutta to handle this communication.
I would like to register a ping to be sent periodically, to check if the connection is still alive. I'm using Wireshark to check the network traffic. This is a screenshot of the result that Wireshark is showing:
From what I see here, I believe that the ping is being sent, and the pong is being received.
A snippet of my code is:
private void keepAlive() {
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
Runnable runnable = new Runnable() {
public void run() {
Log.d(TAG, "Pinging...");
WebSocketHandler.this.webSocket.ping("LALALA");
}
};
pingScheduledFuture = scheduler.scheduleAtFixedRate(runnable, 0, PING_PERIOD,
TimeUnit.SECONDS);
}
The onPongReceived method just prints into Logcat
#Override
public void onPongReceived(String s) {
// TODO here I'm aware if connection is still alive
Log.d(TAG, "Pong received! " + s);
}
However, Pong received! is never printed! Also, if I put a breakpoint there, the app will never stop executing at that point
Anyone has any idea on what may I be missing here?
Best regards and thanks in advance
I'm not familiar with AsyncSocket but a quick google revealed that you have to register a callback setPongCallback() somewhere for your pong to be received. Are you doing this? You're not showing a lot of code.
The problem was extremely lame, but here's the solution. I forgot to set the callback to the websocket, like this:
WebSocketHandler.this.webSocket.setPongCallback(WebSocketHandler.this);
And then, the pongs were correctly received.

Use UncaughtExceptionHandler to do what Android usually does plus extra functionality I need

I am working on an Android library where I need to perform some action if an uncaught exception is seen.
I am successfully setting my uncaught exception handler and running the code I need, which is to post some information to a web server, but after my part is finished I want the app to then do what Android usually does, i.e. display a dialogue to the informing them that it has crashed and then exit the app, and post the details to the Google Play Developer Console.
At the moment, my uncaught exception successfully posts to the server, but then keeps the app running but in a bit of a weird state as the thread has party disappeared, where usually, if my an uncaught exception is thrown, then Android closes the app.
Below is how I am doing my uncaught exception handler:
private static void setUnhandledExceptionHandler()
{
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()
{
#Override
public void uncaughtException(final Thread thread, final Throwable) {
CrashReporter.ReportUnhandledCrash(((Exception) ex));
Looper.loop();
}
});
}
Basically, what I want to do is, have my app post to to my server via my unhandled exception handler and then quit the app, in the same way that Android usually does, i.e. display a force close error the use and close the app.
I had a similar problem when tracking to GoogleAnalytics - most Exceptions got lost when i tried to report them in the default handler. So i cached them and reported them on the next startup.
The second trick (and this hopefully answers your question) to let the Application crash as it should is to store the 'old' DefaultUncaughtExceptionHandler and pass the exception to it.
The onCreate method below is the one of my Application class
#Override
public void onCreate()
{
super.onCreate();
trackCachedException();
setupExceptionHandling();
}
private void setupExceptionHandling()
{
_systemExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.UncaughtExceptionHandler myHandler = new Thread.UncaughtExceptionHandler()
{
#Override
public void uncaughtException(Thread thread, Throwable ex)
{
cacheException(thread.getName(), ex);
if (_systemExceptionHandler != null)
{
_systemExceptionHandler.uncaughtException(thread, ex);
}
}
};
// Make myHandler the new default uncaught exception handler.
Thread.setDefaultUncaughtExceptionHandler(myHandler);
}
The only way I can see how you can do this is neither clean nor pleasant, so I'll say sorry before we begin...
The order of operations is:
Store the uncaught exception
Log the exception as you have done previously
Deregister your uncaught exception handler, by calling the set method with null.
Launch a new thread that throws your stored exception.
You are effectively throwing the same exception twice, but removing the recovery mechanism on the second throw.

android while loop alternative

This is my first Android application and I am finding troubles with while loop, I am trying to use a while loop on my Android application but the application freezes.
What I'm trying to do is track the user location (using onlocationChanged) and keep querying on the location until the query returns a result. It's a GIS application so I am going to describe the application behavior:
the application keeps tracking the user position using a listener "onLocationChangedListener" and store it in a variable "myPosition". I am using a boolean"noResults=true". I will use a method "query(myPosition)" in the while loop, this method has a callback that when a result is found, and changes a boolean "noResults" to false. the loop will keep on until "noResults" is false (that means query's callback changed the boolean's value)
, here's what I did:
while(noResults)
{
//myPosition keeps changing
query(myPosition);
//query has a callback that when a result is found it changes noResults to false
}
I resolved the problem using a "Handler" that query the Feature Layer every 5 seconds, this stops the main thread from generating application not responding error:
Handler m_handler=new Handler();
Runnable m_runnable;
m_runnable = new Runnable(){
public void run() {
//query code here
m_handler.postDelayed(m_runnable, 5000);
}
};
m_handler.postDelayed(m_runnable, 0);
running while loop codes on the main thread freezes the UI, and makes all other processes pause making your app unresponsive use
Threads..
also note that the while loop you are running is running on a default Thread termed as the ui thread so in short run while loops on separate threads..
eg..
new Thread(new Runnable() {
#Override
public void run() {
// Your hard while loop here
//get whatever you want and update your ui with ui communication methods.
}
).start();
for ui communicating methods
View.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "updated ui", Toast.LENGTH_LONG).show();
}
});
the view could be any views you are updating..
also like #TehCoder said you could use asynctask but asynctask is not meant for long workaflow work there are 3 of them but i can't recall the last one
Maybe you should use an AsyncTask? I'm not quite sure what your problem is tho.
Loop is not a problem in android (or any language).
There are two scenario might be reason for your freezing,
If you run network call in api, android throw error and crashes. You have to do network related calls in Aysnc Task ot threading
Use try throw catch and exception cases to avoid app crashing and better coding skill.

UI thread issue

I've been developing a few basic Android apps recently and notice a slightly odd behaviour which I'm sure is my own wrong doing.
The problem seems to lie with the main thread which I'm using for both updating the UI and some processing such as sending a message via Bluetooth.
Let's say I have the following:
public void sendMessage(){
updateUI();
sendBtMessage();
}
public void updateUI(){
txtView.setText("Sending message");
progressbar.setVisibility(View.VISIBLE);
}
public void sendBTmessage(){
... connect to BT and send message here
}
As I run my code it appears to be running sendBtMessage first as the UI update appears after the message is sent(I would like it before sending the message). Is this because the main threads priority is to do the heaviest work-load first?
Should the main thread be used for only updating the UI?
Any Suggestions or advice would be appreciated.
Turns out I needed to handle the sendBTMessage on a new thread such as:
new Thread(new Runnable() {
public void run(){
...processing
}
}).start();
and the UI runs smoothly using
RunOnUiThread(Runnable)

android.os.NetworkOnMainThreadException, while NOT actually running in MainThread

There are many previous questions regarding the android.os.NetworkOnMainThreadException exception , which is essentially a protective approach by android to prevent us from freezing UI.
Opening a socket from another thread (hence, not the MainThread) should solve this issue:
Thread t = new Thread (new Runnable() {
#Override
public void run() {
try{
Socket socket = new Socket ( SOME_IP_AS_STRING , SOME_PORT_AS_INT);
// do some IO with socket
}
catch (Exception e) {}
}
});
t.run();
However, this code throws the mentioned exception - android.os.NetworkOnMainThreadException,
and when debugging (using the Android Studio), it looks like run() is running under the MainThread after all, which makes no sense.
where do I got it wrong?
You're calling .run() which actually will run the Thread in your main UI Thread. You need to call .start() instead to avoid it.
The exception that is thrown when an application attempts to perform a networking operation on its main thread. Try to run your code in AsyncTask , For more detail refer here

Categories

Resources