I'm trying to execute a service without an activity, yet ADV emulator seems to not response to my application. As written at the title I would like to have Service applications that will run constantly without UI/activity.
Here is my code: AndroidManifest.xml:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false" >
<service android:name=".service.PublicStartService" android:exported="true" />
</application>
</manifest>
Java File
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) {
}
}
}
}
}
Any Idea what do it miss to make it execute?
Thanks!