我试图在没有活动的情况下执行服务,但 ADV 模拟器似乎没有响应我的应用程序。正如标题所写的那样,我希望服务应用程序能够在没有 UI/活动的情况下持续运行。
这是我的代码: AndroidManifest.xml:
public class PublicStartService extends Service {
private static final String TAG = "ServiceLog";
// The onCreate gets called only one time when the service starts.
@Override
public void onCreate() {
Log.i(TAG, "onCreate ");
}
// The onStartCommand gets called each time after the startService gets called.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String param = intent.getStringExtra("PARAM");
ExecutorService executorService = Executors.newFixedThreadPool(4);
Runnable worker = new MyRunnable();
executorService.execute(worker);
return Service.START_STICKY;
}
// The onDestroy gets called only one time when the service stops.
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
class MyRunnable implements Runnable {
@Override
public void run() {
while (true){
Log.i(TAG, "THREAD THREAD THREAD ");
try {
sleep(500);
} catch (InterruptedException ignored) {
}
}
}
}
Java 文件
<plugin>
<groupId>io.takari.maven.plugins</groupId>
<artifactId>takari-lifecycle-plugin</artifactId>
<version>1.11.10</version>
<extensions>true</extensions>
<configuration>
<debug>${javac.debug}</debug>
<encoding>${project.build.sourceEncoding}</encoding>
<source>${javac.source}</source>
<target>${javac.target}</target>
<proc>proc</proc>
</configuration>
</plugin>
}
知道让它执行有什么遗漏吗?
谢谢!