How to debug an Android Background Service? - java

I have been developing a PhoneGap application for Android which contains a Background Service. My question is: How can I debug this service? Is it possible to debug using an AVD and go step by step? or can I use my own device to achieve that?
Thanks!

Yes, it can be done using AVD or device. Check out http://www.helloandroid.com/tutorials/how-debug-service and Debugging a service.

You can debug your service by putting a single statement , I am mentioning it here :
#Override
public void onCreate() {
super.onCreate();
//whatever else you have to to here...
android.os.Debug.waitForDebugger(); // this line is key
}

You can use your usual logs -for not in-depth debugging- inside the service and then use monitor tool to view those logs when the app is in the background or closed.
To open the monitor tool:
Open SDK( your SDK folder ) >tools>lib>monitor-x86_64
Open your monitor tool and select a device and you can view the loggings and search by tag as you do in your usual debugger.

Specific to intellij Idea, although there may be an equivalent solution for eclipse also,
I am adding a few more points to Shishupal's answer which worked for me, We need to add android.os.Debug.waitForDebugger(); in the service code.
But along with above, we need to uncheck "Deploy Application"
and select "Do not launch Activity".
In version 12.1.4 it looks like this:

Look for where your background service starts, for example
public BGcheckService() {
Log.d("BGcheckService: ", "starting service");
}
Then insert one line:
public BGcheckService() {
android.os.Debug.waitForDebugger();
Log.d("BGcheckService: ", "starting service");
}
Besides this, if your background service is in a separate process, then when you press the debug button to start debugging your background service, you also need to wait till the background service has started, then press the button to attach to process (see screenshot for Android Studio 3.6.1 .. it is the 3rd button to the right from the debug button). You will be given a choice of processes to attach to, one of which would be the background service's separate process.
NB:
Without the line android.os.Debug.waitForDebugger();, the background service would just start without waiting for the debugger to attach.
Even with the line android.os.Debug.waitForDebugger();, you still need to attach the debugger to the process of the background service.
The timing of when you press the button to attach to the process, needs to be after the background service has started (while it is waiting at android.os.Debug.waitForDebugger();), otherwise the process would not exist and you would not be able to select it.

Related

Is it possible to make a timer run in the background app even if the app is not in main focus?

i want my timer to keep running in the back of the app itself and when the user will re-open the app, it will go directly to that activity with the timer still running
You probably want to implement a Local Service. The usage is documented here: https://developer.android.com/reference/android/app/Service.html
By using service class and bind the service so that you can in background until you end service

Is it possible that the service still runs after the app got killed with task manager?

Hello im triying to run a service in background that it doesn't stop when app is destroyed by task manager. The idea of the service is verify every "x" min if there a new insert in a database that i got in a server.
The service is running great even if i close the app but when i use the task manager to destroy my app all the threads are closed too.
So i want to know if its possible to run a thread that ask in background forever unless user cancel it in the app itself, that ignore the destroy caused by task manager so in the future i can use notification bar to tell the user that a new insert happened in the database.
Tryed:
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
askServer(); // i made a timertask that ask every "x" minute
return START_STICKY;
}
As i read START_STICKY should run again the service if it get killed for some reason and i know that this can be done since some app get closed by taskmanager and still get notifications from it as whatsapp,bbms and others. Please tell me if im wrong in anything and thank you for reading!.
UPDATE: Im not trying to break any law or security rule from Android and im not trying to ignore the stoping services option from an app in settings. I want that the service that listen for new incoming "events " inserts in my case keep running after user used the interface that appear when you press home for a while :
UPDATE : sorry for talking to much about this app but is the one that i can use as an example. In whatsapp when i close the app by the interface that i showed above the process and services are killed but after a couple of second they relaunch, this is exactly what i want to do to keep user informed about database events. From setting you still can stop the service without problem or even i can put the option in the app itself to stop notifiying.
Is a bad implementation call in OnDestroy() method an instance of the service so it relaunch after destroy?
UPDATE : welp looks like my service is still running on background after i close the app. I will just have to work on my service design to not waste battery life and resources. Also i was using the log.i() to check if service was running, looks like when main process closes i can't use log or toast just notifications ( still not implemented) because the service is there running just won't show in log .
UPDATE : now is working using using startForeground(0, null). In future i will send a notification to show when a event on database happen building it and calling startForeground(1, notification).
For services, look at Settings -> Applications -> Services. and see if it is running.
However, poorly designed services may run more often or perform syncing operations. So yes it is possible.
I had a problem similar to this when developing my first android game; force-stop was the only way to kill it.
START_NOT_STICKY will kill the background service when you swipe the app away from the task manager. START_STICKY is, as the name implies, "sticky", Meaning it sticks to the android system even when the app is gone.
That's from my experience, anyway.

How to force an application to stay open?

I have seen apps like Lookout, JuiceDefender, and MagicJack run in the background indefinitely, unless force closed by a user directly through the task manager. (And even then, in Gingerbread, it wouldn't close unless you browsed to the application that was running under "Downloaded Apps" in the settings and force closed it once you were at the menu where you have options to manage the app like "Clear Memory" and "Force Close".
I am wondering how this is accomplished? I need to do something similar for an app of mine but I don't know how to avoid the Android OS's automatic task killing.. And don't say it's not possible because if that were true, JuiceDefender, MagicJack, and Lookout would not work.
What you can have is a service that stays alive indefinitely. You achieve that returning Service.START_STICKY on your Service's onStartCommand method.
Whenever the os needs resources and chooses to kill your app, your service will be respawned as soon as the resources are available again.
Bear in mind that having an application that is continuously alive will result in consuming the phone's battery. You should (at least) notify the user with a notification that your app is still alive in the background.
On top of that, you can register a broadcast receiver for the BOOT_COMPLETED event in order to restart your service while the device gets restarted. Yet, bear in mind that this could result in eating the phone's battery and so be careful on what you are doing in the service.
I believe these apps are launching a Service when their Activity get started (i.e when onCreate() is called).
A Service keeps running when the application get paused. When the Service is launched, you may return START_STICKY in your onStartCommand.
Also, to prevent a Service from being killed by Android's memory killer, you can specify that your Service is important to the user by calling startForeground(). Android Developers website states that :
A foreground service is a service that's considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory.
I am creating an app and I have to use one or more of the following super functions inside OnCreate():
onDestroy()
onPause()
onResume()
onSaveInstanceState()
to close an app completely from the memory. And also do not use Activity.finish() method. Usually Android does a pretty good job in closing the app when memory is needed, called pop out of stack and not recommended to forcefully stay in memory, unless there is a very very good reason to. Hope it helps.
You can also check the Android DOC website for more information and examples to your request.
You need to start a service. Services runs in background and is useful to push alerts.
This some links about it:
http://developer.android.com/guide/components/services.html
http://www.vogella.com/articles/AndroidServices/article.html
In the service onStartCommand method return "START_STICKY".
http://developer.android.com/guide/components/services.html
/Thomas

When a web service call was invoked in BlackBerry, application hangs

I am creating my very first blackberry application that tries to connect to a rest web service. I tried the example I found in the internet. Please refer to this link: http://mobile-development.org/index.php/blackberry/how-to-call-restful-web-services-in-blackberry
I tried to implement it in my simple BlackBerry application which is the one that is automatically created when you create a new BlackBerry project in BlackBerry Plug-in for Eclipse IDE. I just placed the code (literally copied and pasted it) in my button, that when clicked, will execute such code. But, when I click the button, the application hangs.
When I implement a code that simply outputs "Hello" in the output log, the application works perfectly fine. What is the reason behind it? Do I need to run the web service call in a separate thread? Please help. Thank you in advance.
--------------------PLEASE READ BELOW----------------------
I noticed that my application hangs because it waits for a response from the web service call of at least 2 min. I read through this: http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is_-_Different_ways_to_make_an_HTTP_or_socket_connection.html?nodeid=826935&vernum=0 -- and added "deviceside=true" at the end of the url (http://yourwebserviceurl.com;deviceside=true"). It works fine now. Maybe the proper implementation for this is to do the task in background or in a separate thread and set the timeout which I do not have a knowledge on it yet. I'm still confused on whether to set the deviceside to true/false. Should I set the deviceside to true when I'm running my app in a simulator then just change it to false if I want it to run in an actual device? That is for me to discover for now or you can help me out on this one as well. ;-)
The Code they have given is in for HTTP connection , It is totaly fine, But what we need to do call this code in a thread , because ui works also in thread by which by which it got stuck.
So you need to use thread concept here.
Look at Samples provided into
Eclipse helios\plugins\<sdk version>\components\samples\com\rim\samples\device\httpdemo
Whenever you wants to update UI In a background thread , use
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
<Update UI>
}
});
I hope it may help you .

Debug android app when it starts on device

I have an android app that is opened by a URL on my website. What I am looking to do is, attach Eclipse in debug mode when the app starts. I can start the app in debug mode from Eclipse, but I do not know how to get Eclipse to start when the user/another app starts the app on the device.
Add a call to android.os.Debug.waitforDebugger() on the onCreate of your launch activity. This will make you app wait for a debug to attach whenever it is launched.
If you look in detail, there's a moment when Eclipse tries to couple the runtime session with the debugger so it starts listening to it until the coupling is made. I guess you'll need to trigger that whenever you open your app.
My question would be, why do you need that? Which are the different conditions when you start the app from the web that make it necessary to debug from there? If there are some, is there any possibility to fake them with any constants or db data?

Categories