Trap all http request in android by creating a service? - android

I am working on an android app in which I want all http request sent by android phone .Is it possible in android by creating a service?

You can create a service to do so, but it will make your code much more difficult, because services have different lifecycles than activities and fragments, they can exist even if the UI is not running. I give you the advice to handle it in your application code. One good library is OkHttp or if you want something more abstract you can go with Retrofit

Related

what is the best/preferred approach to implement multi-threading in android app

I'm a beginner in android development and I'm trying to implement an android udp client, which connects to a java server and sends/receives some packets from it.In this process it collects some data (like round-trip delay etc), which is used to measure the QoS of that particular network. I have tried implementing the connection and sending/receiving data using Java Threads, but the application crashes, and hangs if i try to use more than 2 threads. So I'm looking for alternatives. While going through this site as well as some other links I found that in android multiple threads can be implemented using AsyncTask, Handler etc. Also I found that the Service class also helps to run a background service in an app. Please suggest which approach among these would be the best to achieve my purpose.
Thanks in advance.
You can use AasyncTask to do this and as you mentioned service may be useful too, where u can let your application do whatever it wants in background , if user needs to use application by its interface then AsyncTask must be used to avoid Crashing
There is not one right answer that can be applied as a broad stroke to how to do Android multi-threading. There are a few different ways to approach it based on what your specific needs are.
Any long running, blocking call, in Android will result in the application crashing.
The most common solution is to use an AsyncTask though. For example, when I want to make a call out to a web API endpoint for some XML data within an Activity I would in this case use an AsyncTask and kick off the calls from within doInBackground.
This is not an appropriate solution though if the wait time is longer, or possibly an unknown wait time. Or in a situation where there will always be waiting such as a message queuing service. In this type of situation it may be best to write a separate app based on extending the Service class. Then you can send/receive notifications to/from the service from your primary application in a similar manner to how you would communicate with a web service.

Notification upon ended parsing of web response

I'm primarily an iOS developer, but I'm creating an Android version of one of my apps, so my question will likely have an easy answer...
My app is primarily based on data that I get from SOAP requests, and I often need to update UI elements based on these requests, so I need to inform a class of when a request has ended...
In iOS this can easily be done by sending a notification with NSNotificationCenter, and I'm looking for something similar (or really anything that will accomplish the same)..
A little example:
When the app opens, the main activity starts a class(this class sends a request to the server to authenticate the user), and when that class is done parsing the gotten information, it should notify the main activity that it's done...
Since the classes that perform the SOAP requests aren't activities, I can't use "StartActivityForResults/onActivityResult" - so how would I go about doing this in Android?
You can accomplish this with an AsyncTask. You can use onPostExecute to notify anything you like. Here's a small tutorial:
http://droidapp.co.uk/?p=177
Hope it helps

How to design a remote controller for an android application?

I've written an android application, and I'd like to make it controllable to other machines by sending HTTP request to the device that run my application. I've written a tiny HTTP server and made it start when my application is started. I know I could translate HTTP requests and send messages to various activities to perform UI operations, that need to add listener to all my activities. But in order to make the remote controller code reusable, I hope separate remote controller code from existing application code and thus I need to find a way to make as less as change to the application code to make it be remote controllable.
Could anyone share your ideas?
I dont get what you are asking tbh but in one of application that my friend create, he just implement a mouse pointer which works from any android phone. With that application he can manage to use the android tv.
When he was implementing that app, he took advantage of socket programming and send messages from remote controller, in that situation a phone, to other device and fetch the data in there. In my opinion if you follow such a manner you just dont need to apply so many changes in your other applications. It is all communication in the end.

Can local services make method calls?

I have just began researching services in Android. What I'm trying to do is remotely call a method within my application. How I believe it will work is the service will listen for requests from the server in the background, then depending on the request, it will call a certain method within the application. Is this possible to do with a service? or are there better ways to do this?
I'm not asking for implementation of this, just confirmation that it could work and is the best way.
Thanks
Local service can call any functions you want inside your application.
This might not be the best application design, but this certainly will work. Local service is equivalent to any other locally created object. The difference is that service is created by Android System, not by your code.

first android service

I creating a small application that will basically use a background server to send data over HTTP. I dont know how to create services. Secondly there will be a couple of activities in my application. I want the activities to display a Context Menu when data becomes available. How can i do both. I have search for a while but the code i keep getting dose not seem to run on 1.6 api. How can i create the service and how can my activities listen to updates so that when a update is available they display a message.
NOTE: I do not need help on the HTTP part and the server part only creating the service and my activities listening to updates.
Kind Regards,
Give the Service docs a very good, thorough read.

Categories

Resources