1

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!

Tal.Bary
  • 450
  • 2
  • 16
  • Where do you start your foreground service? – shmakova Apr 22 '21 at 12:46
  • That's what I try to figure out, that AndroidManifest.xml I see common at many places. the only thing I see is at android studio choose No Activity at run settings BUT I just can't see that service or application actually executing. Only weird helpless errors. – Tal.Bary Apr 23 '21 at 11:58
  • https://stackoverflow.com/questions/7690350/android-start-service-on-boot/7690600#7690600 – shmakova Apr 23 '21 at 15:29

0 Answers0