I already have a socket communication interface which works fine in 2.3 version, but It doesn't work on 4.0.3, I've heard that it's not possible to make this kind of communication in the same thread as UI in this version so, I'm beginner in android and didn't found any example on this type of communication in 4.0.3 version, do you have some example or can tell me how to do it ?
Thanks in advance
The easiest way to do work on a different thread is to use AsyncTask. Be aware of the fact that your AsyncTask will break if the context gets destroyed, for example if the screen rotates. You will need to handle such events, as well.
You could read this SO question and start from there.
Here's a quote from the AsyncTask docs:
AsyncTasks should ideally be used for short operations (a few seconds at the most.) 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 pacakge such as Executor, ThreadPoolExecutor and FutureTask.
when you are using ICS or JB, you are restricted from opening network connections from within your main activity. you will get an opaque error message regarding network permissions. this is true from ICS onwards (so froyo and GB would not complain).
i cant repost my answer to another quite similar StackOverflow question here (it is considered spamming by moderators), but you can CHECK IT OUT HERE.
i posted a functional send and receive socket connector client using asynctask there.
Related
I'm looking for a way to send SCEP requests from android devices. I found some incomplete code in this SO-question. Of course I could do all the work and boilerplate code myself, but I wonder if there's a more standard way to enroll android devices to our PKI. Googling around I found a lot of hits regarding jSCEP not being supported on android, but these hits are quite old and jSCEP does not seem to be a very active project.
So my question is: Do newer versions of android (marshmallow and upwards) support a standard way to create and send SCEP requests?
OK, after some trial & error I found out that it is possible to use jScep on Android following the instructions on the github site of jScep.
The only differences are the construction of the client where i had to exchange the ConsoleCertificateVerifier with OptimisticCertificateVerifier to make it work. As the scenario takes place in an enclosed environment this doesn't raise any security issues for our case.
The other changes were to run the request in it's own Thread and handle the result in a Handler to avoid blocking the device while requesting.
In the long run we may fork jScep as it seems to be not well maintained, but that's another cup of tea. :-)
Why does a service not run in it's own thread under Android? And what were the design considerations?
It is all about Flexibility: I assume you are familiar with so many Frameworks like for example e.g. Volley, Retrofit etc. These Frameworks has their own Thread implementation behind scene so if Service by default is executed in a separate Thread then it is overkill. Why? because the threading is handled by the framework already so why the need to have another thread?
See also Why is creating a Thread said to be expensive?. And put also into consideration that you are doing Threading in an Android device and not into a powerful Desktop.
Take a look at IntentService I think this is that you are looking for.
It gives you more flexibility. For example you could run each request in a seperate thread with special priorities or you could store the requests in a queue and only start the next thread if the previous one has finished. A IntentService provides a default configuration that in the most cases fits your needs from scratch.(http://developer.android.com/guide/components/services.html). Such decisions depend on your use case.
I have been discussing a problem on the Indy forums related to a thread that is not terminating correctly under Android. They have suggested that there may be an underlying problem with TThread for ARC.
Because this problem is holding up the release of a product a work around would be to simply forcibly terminate the thread. I know this is not nice but in this case I cant think of a side effect from doing so. Its wrong but its better than a deadlocked app.
Is there a way to forcibly terminate a thread under Android like TerminateThread does under windows?
Martin
Well, I don't think harshly terminating threads is advisable, but that notwithstanding, from what I read of the System unit, you should be able to use this:
uses Posix.Pthread;
...
pthread_detach(pthread_t(TheThreadObject.ThreadID));
Untested, mind.
I am writing an Android application that has to download quite a lot of image files; the previous dev wrote it using various HttpClient type classes. These give no progress information whatever, and if the download fails part-way through there is no way to recover the missing bits (as far as I can find out).
We want to be able to report progress to the user, particularly if it is slow, and we want to be able to resume downloads, so I reimplemented this using HttpUrlConnection. It works fine single-threaded, but not multithreaded: I don't know what happens exactly but the images are getting corrupted. Android says that instances of this are indeed not thread safe.
As far as I can tell this means we have to choose between multithreaded downloads (which my colleague reports are slightly faster) and the ability to resume. Is there any way to get both on Android, short of implementing my own, thread-safe HTTP connection class?
Two things come to mind. The AsyncTask class (see: Painless Threading) is built for this sort of thing, and the new DownloadManager class was developed as a rather robust method that restarts if necessary, persists through device restarts, etc. Obviously it really depends on your situation, but give those a look and see if they're something you need.
I'm trying to learn how to use Networking API in Android (or how to make the app network enabled).
I looked in the following places:
1. Coder-Darcey book "Android: wireless application development" has a short chapter on this. But it is for an older version, 1.5, of Android. I'd like to refer to a more recent & more detailed information.
2. Strangely the developer.android.com site has nothing at all on this.
3. Searched SOF on this, & found this question. The link it refers to is WAY too complex for me to understand.
Anyone happen to know a good reference/website/tutorial for this important topic?
Thanks!
It wasn't very different until honeycomb - but then they stopped you from making network calls in the main thread.
Making a receive thread is pretty simple - but a tutorial is really in order for a decent send thread - ensuring no syncronisation issues between adding stuff to send and sending it....
--
found a work around for the "network in main thread" check... http://www.vogella.com/articles/AndroidNetworking/article.html#networkoverview_apis