0

I have written a simple code to check the use of service .

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("msg","Service Started");

    if(intent == null)
    Log.w("msg","Service Intent NULL");

    g = (GlobalVar) getApplicationContext();

    try {
        Thread thread = new Thread(new Job());
        thread.start();

    } catch (Exception ex) {
        Log.e("msg", ex.getMessage());
    }

    return START_STICKY;
}

There are two buttons in my activity. One button starts the service using startService(Intent intent) method and other one stops it using stopService(Intent intent) method. 1. I pressed the startButton and the msg Service Started is printed in Logcat. Intent will not be null this time. 2. Now , I go to Emulator's Settings>Applications>Running Services and locate my service and stop it. The msg Service Stopped is printed in the Logcat which I have written in the onDestroy() method of the service.

Here is the part which I don't understand.

The Android System restarts the service itself after some seconds and this time the msg Service Started is not printed. The documentation states that intent will be null. BUT the msg Service Intent NULL is also not printed.

How do I know that android system restarts the service?

I have printed some messages in the Thread that my service starts . Those msgs start printing automatically in the logcat after few seconds of stopping the service from the emulator settings.

Any ideas why those LogCat messages are NOT being printed ?

4

1 回答 1

0

我猜你应该对下面给出的这些东西有一些基本的了解......

Log.e:这是为了当坏事发生时。在 catch 语句内部等位置使用此标签。您知道并且发生了错误,因此您正在记录错误。

Log.w:当你怀疑有什么可疑的事情发生时使用它。你可能没有完全进入错误模式,但也许你从一些意想不到的行为中恢复过来。基本上,使用它来记录您不希望发生但不一定是错误的事情。有点像“嘿,这件事发生了,很奇怪,我们应该调查一下。”

Log.i:使用它来将有用的信息发布到日志中。例如:您已成功连接到服务器。基本上用它来报告成功。

Log.d:将其用于调试目的。如果您想打印出一堆消息以便记录程序的确切流程,请使用它。如果要保留变量值的日志,请使用它。

Log.v:当你想对你的日志完全疯狂时使用它。如果出于某种原因您决定在应用程序的特定部分记录每一件小事,请使用 Log.v 标记。并参考这里

我相信这会帮助你...谢谢...

于 2013-04-16T05:00:29.840 回答