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