Why does TileService wait several seconds to start an Activity?

1.4k views Asked by At

I'm using a TileService as a shortcut to open my app's Activity. It does only that, and has no state.

It looks like this:


class QuickAccessTileService : TileService() {

    override fun onClick() {
        super.onClick()

        val intent = Intent(this, SlideOverActivity::class.java)
            .addFlags(FLAG_ACTIVITY_NEW_TASK)

        startActivityAndCollapse(intent)
    }
}

and it's declared in AndroidManifest as:

<service
    android:name=".service.QuickAccessTileService"
    android:icon="@drawable/ic_home"
    android:label="@string/tile_label"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">

    <intent-filter>
        <action android:name="android.service.quicksettings.action.QS_TILE" />
    </intent-filter>
</service>

As it stands, this works. However, when my application is stopped, the next time I try to open it via the quick settings panel, it takes several seconds to start the activity.

Here's what I know:

The Activity in itself isn't slow to start. Trying to open it via the launcher instead makes it quite clear.

The Service seems to be what's taking a while to start before onClick is even executed. It makes sense; the Service is probably not kept running in the background doing nothing while the app isn't running. However, this means that when the system detects a click on my Tile, the Service has to be recreated first, which takes way too long.

I'm not sure where to go from here — if my guesses are even right.

EDIT: As an important addition, I can reproduce this on a OnePlus 7 Pro running on Android Pie. This might be a OnePlus-specific issue as I cannot reproduce it on an emulator.

1

There are 1 answers

0
Doston Hamroyev On
@RequiresApi(Build.VERSION_CODES.N)
class TileSettingClass : TileService() {

    override fun onClick() {

        super.onClick()

        val tile = qsTile
        if (tile.state == Tile.STATE_INACTIVE) {
            tile.state = Tile.STATE_ACTIVE
            val intent = Intent(this.applicationContext, YourActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.action = "yourAction"
            applicationContext.startActivity(intent)
        } else {
            tile.state = Tile.STATE_INACTIVE
        }
        tile.updateTile()
    }
}

Manifests

<service
    android:name=".main.service.TileSettingClass"
    android:icon="@drawable/ic_tile_setting"
    android:label="@string/milliy"
    android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
    >
    <intent-filter>
        <action android:name="android.service.quicksettings.action.QS_TILE" />
    </intent-filter>
</service>