Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为android创建一个简单的打印到后台日志的循环服务?_Java_Android - Fatal编程技术网

Java 如何为android创建一个简单的打印到后台日志的循环服务?

Java 如何为android创建一个简单的打印到后台日志的循环服务?,java,android,Java,Android,我正在尝试创建一个简单的服务,每隔几秒钟就打印一次日志,即使应用程序重新启动,尽管我已经对此进行了研究,但我仍然困惑于如何才能做到这一点。如果有人能给我看一些代码,我会非常感激的,我可以用这些代码来做这件事 日志记录 背景 ServiceClass.java(IntentService在新线程上启动,我们不需要在其中创建新线程。) Mainifest(内部应用程序标记) public class IntentServiceDemo extends IntentService {

我正在尝试创建一个简单的服务,每隔几秒钟就打印一次日志,即使应用程序重新启动,尽管我已经对此进行了研究,但我仍然困惑于如何才能做到这一点。如果有人能给我看一些代码,我会非常感激的,我可以用这些代码来做这件事

日志记录

背景

ServiceClass.java(IntentService在新线程上启动,我们不需要在其中创建新线程。)

Mainifest(内部应用程序标记)

  public class IntentServiceDemo extends IntentService {
          int currentState=0;

      public IntentServiceDemo() {
        super("IntentServiceDemoWorker");  // super the name of worker thread, it is necessary.
    }
    @Override
     protected void onHandleIntent(Intent intent) {
                while (true) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                       ++currentState:
                    Log.d("vabhi", "currentState : " + currentState);
                }  // End of while
    }
}
<service android:name=".IntentServiceDemo" android:exported="false"></service>
public void startIntentService(View v)
{
    Intent intent = new Intent(MainActivity.this,IntentServiceDemo.class);
    startService(intent);
}