Search code examples
androidandroid-serviceandroid-7.0-nougatandroid-7.1-nougat

Android AppTileService onClick not get called


For my android app I have added an AppTile. Most of the time this AppTile is not clickable. Its icon is grayed out, such as disabled or unavailable icons (https://developer.android.com/reference/android/service/quicksettings/Tile.html#STATE_UNAVAILABLE)

In my Logs this line is never reached

Logger.d(getClass(), "onClick()");

But I have tested that the methods onStartListening() or onStopListening() are called every time the AppTile becomes visible or hides.

AppTileService.java

package my.package

import android.os.Build;
import android.service.quicksettings.TileService;
import android.support.annotation.RequiresApi;

import my.package.utils.Logger;

@RequiresApi(api = Build.VERSION_CODES.N)
public class AppTileService extends TileService {

    @Override
    public void onClick() {
        Logger.d(getClass(), "onClick()");
        if (isSecure()) {
            Logger.d(getClass(), "isSecure = true");
            NotificationHandler.showDirectReplyNotification(this);
        }
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.package"> 
   <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service
            android:name=".AppTileService"
            android:icon="@drawable/tile_icon"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
            <intent-filter>
                <action android:name="android.service.quicksettings.action.QS_TILE"/>
            </intent-filter>
        </service>

    </application>
</manifest>

Can someone tell me what this could be, or whether I have forgotten something to implement?


Solution

  • You need to indicate that the tile is active, in one of two ways.

    First, your <service> could have <meta-data android:name="android.service.quicksettings.ACTIVE_TILE" android:value="true" />.

    Second, in onStartListening(), you can update the tile state to be active, as I do by means of an updateTile() method in this sample app from this book:

      private void updateTile() {
        Tile tile=getQsTile();
    
        if (tile!=null) {
          boolean isEnabled=
            getPrefs()
              .getBoolean(SettingsFragment.PREF_ENABLED, false);
          int state=isEnabled ?
            Tile.STATE_ACTIVE :
            Tile.STATE_INACTIVE;
    
          tile.setIcon(Icon.createWithResource(this,
            R.drawable.ic_new_releases_24dp));
          tile.setLabel(getString(R.string.app_name_short));
          tile.setState(state);
          tile.updateTile();
        }
      }