Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick Settings Toggle in Android N

I'm trying to add a quick settings toggle to my app in Android N. The quick tile shows up, but it doesn't do anything when clicked. I can see the visible feedback when touched, so I know that it is recognizing the click, but it doesn't do anything when clicked.

Here's my service code:

public class QuickSettingTileService extends TileService {

    public QuickSettingTileService()
    {
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startID)
    {
        //some setup code is here

        return START_STICKY;
    }

    @Override
    public void onClick()
    {
        Context context = getApplicationContext();

        Toast.makeText(context, "Quick Setting Clicked", Toast.LENGTH_SHORT).show();
        //Toggle code is here
    }
}

My manifest has the code almost directly copied from the documentation. Only slight modifications have been made:

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

The service is started upon opening the app:

Intent serviceIntent = new Intent(this, QuickSettingTileService.class);
startService(serviceIntent);
like image 219
Randy Avatar asked Mar 12 '23 19:03

Randy


1 Answers

Just remove these lines from your QuickSettingTileService class

@Override
public IBinder onBind(Intent intent)
{
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startID)
{
    //some setup code is here
    return START_STICKY;
}

There is no need to override onBind() or onStartCommand() on a TileService.

Also, you don't need to explicitly start this service. The permission and intent-filter in the manifest will make sure Android OS will start your service when your tile is added to the Notification bar.

like image 57
murki Avatar answered Mar 24 '23 22:03

murki