2

I try to create a background service with no user interface and I created it with BroadcastReceiver and added my necessary things in the manifest file. Then I configure Nothing as written in the link. I'm testing on an Android emulator, when I execute my app nothing happen and the service is not starting. Where is my problem?

Here is my manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.myapplication22">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication22">
    
        <service android:name=".MyService" />
    
    
        <receiver
            android:name=".StartReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Here is my BroadcastReceiver

override fun onReceive(context: Context, intent: Intent) {
    Log.i("MYSERVICE","override fun onReceive(context: Context, intent: Intent)")
    if (intent.action == Intent.ACTION_BOOT_COMPLETED ) {
        Intent(context, MyService::class.java).also {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startService(it)

                return
            }
            context.startService(it)
        }
    }
}

Here is my Service

class MyService : Service() {
    
    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent != null) {
            println("MYSERVICE STARTED AUTOMATICALLY")
        }
        return START_STICKY
    }

    override fun onCreate() {
        super.onCreate()
        println("MYSERVICE override fun onCreate() {")

    }

    override fun onDestroy() {
        super.onDestroy()
        println("MYSERVICE override fun onDestroy() {")

    }
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
saulyasar
  • 797
  • 1
  • 17
  • 45

1 Answers1

2

your Service will start only when BOOT_COMPLETED action will be received, so only when app already installed and system restarts. it won't start when you just install app through adb (as you configure to start Nothing when installed)

make some Activity fully transparent with startService in onCreate and just after that finish() call. include <activity declaration in manifest, but without any <intent-filter. then instead of Nothing to run when app install just pick Specified Activity and point on this transparent one, which starts Service and quits after that. without intent-filter Activity won't be visible in launcher and only way to start it will be your configuration in AS

when you will be sending your app to end-users then remove <activity declaration leaving no actual way to run this transparent Activity. only Service can be started then, in current circumstances only when system boots up

btw. your Nothing link assumes your app will be system-belong, moved to system partition/folder. do you have such privileges? (on usual user device this won't be possible, you need root privileges) without that all red-marked lines in <application tag won't work

edit:

step 1:

create file res/values/styles.xml and past below or if present already add below <style tag

<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

step 2: create new Activity and declare in manifest with above style

<activity android:name=".YourActivity" android:theme="@style/Theme.Transparent"/>

if you add below <intent-filter to <activity tag then this Activity shows in system UI (launcher), so just don't do that, placing here just to inform

// launcher icon declaration, not for OPs case
<intent-filter>
    <category android:name="android.intent.category.LAUNCHER"/>
    <action android:name="android.intent.action.MAIN"/>
    // MAIN must be placed with first LAUNCHER declaration
</intent-filter>

note that you can also create multiple <activity tags with LAUNCHER declaration (second and further without MAIN tag), making your app show multiple icons, one per Activity LAUNCHER-declared (you can also change their names and icons as far as I remember)

step 3: inside onCreate just after super.onCreate call start your Service and then quit this invisible Activity

startService(this, MyService.class); // will start service
finish(); // quits activity

step 4: under Edit Configurations... instead of Nothing just pick Specified Activity

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • But the idea is here when i create activity i also need to create app instead of app i want to create Service will work on background and without ui ,is it possible what you write? – saulyasar Dec 03 '20 at 09:29
  • can you write also example from my example? because looks like so complicated – saulyasar Dec 03 '20 at 09:31
  • 1
    you are creating an app, just look on manifest structure, there is ` – snachmsm Dec 03 '20 at 10:12
  • 1
    btw. about APK installing - when someone install your app not-through-AndroidStudio (from apk or from some store, e.g. Play Store) also nothing happens, nothing will run. your current code is starting only on `BOOT_COMPLETED`, so device must be restarted after app installation. if this is your intended behavior then I don't see nothing wrong in declaring additional `Activity` just for `Edit Configurations...` option and easier development – snachmsm Dec 03 '20 at 10:20