-7

I have questions about how it should run a piece of code of a android application.

This piece of code is very important to run all, and requires a certain amount of processing and no iteration with the user.

I have some doubt where it should run, I'm sure it should not be in the Activity or AsyncTask.

Do not know if a thread or a Services, as I think it is not a service in itself. and I have doubts about what would happen if a thread, if the application closed, paused, or close all activities...

user60108
  • 3,270
  • 2
  • 27
  • 43
  • sorry for my english. The question is how should run, to make sure it completes its execution. Although all activities are closed – user60108 May 29 '14 at 05:34
  • 1
    Pls use logcat and try to find out the type of error, if you dont know how to use then see here http://developer.android.com/tools/debugging/debugging-log.html – Robi Kumar Tomar May 29 '14 at 05:41
  • A `service` is a program (just like any other) that lacks a UI, runs in the background and (most commonly) communicates only through log files, it just sits there until a `client` program requires it. If your *piece of code* is just called (with or without parameters) and exits, then you can use an AsyncTask or a Thread. If it needs to *sit* and wait for requests, respond and sit there again maybe you need a `Service`. – arielnmz May 29 '14 at 05:43

4 Answers4

3

If you are unsure that your piece of code is running or not then you can use Log to be sure.

Use Log.i("Classname", "Executed"); if its on your logcat it means it has been executed.

Learn More About Log From Here : Log

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
2

Put the code in a background service, and set that service to auto-start on boot:

  <receiver android:name="com.example.MyService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
  </receiver>

This way you will get a chance to execute some code, without user opening the app UI.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

Ques: How to be sure that a piece of code is executed?

Answer : Simply use Log.i("key", "I am here");

and then go to the logcat and search keyword key, if you found I am here corresponding to key then it means your code successfully executed till this line, else go back and use this where you have doubt in a peace of code in same way, You can use multiple log.i in single activity, even at each line. My personal suggestion always use logcat, if you want to resolve your problem quickly and easily by your self.

Here are the some related points :

Note: Below given contents is not mine, I have taken this from here

    Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statment. You know and error has occurred and therefore you're logging an error.

    Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it."

    Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes.

    Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this.

    Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.

And as a bonus...

    Log.wtf: Use this when stuff goes absolutely, horribly, holy-crap wrong. You know those catch blocks where you're catching errors that you never should get...yea, if you wanna log them use Log.wtf

So special thanks to Kurtis Nusbaum and Jakobud.

Community
  • 1
  • 1
Robi Kumar Tomar
  • 3,418
  • 3
  • 20
  • 27
0

In your functions, add a log so that you can see if it is executed.

Sample:

private void sampleFxn(){
  some code here...
  Log.d("YourTag", "Executed");
}
JayR
  • 441
  • 1
  • 4
  • 16