I am trying to add a custom quick settings panel for my application. I have been following the sample code / documentation from Google, but I am facing some problems. After some searching, I could not find any solutions.
When I launch my application, the tile is displayed in the quick settings panel, but it remains inaccessible.
I need a two-way communication flow with the tile, that is, when the user selects the tile, the application responds, and when the user performs a specific action in the application, the tile user interface switches.
The problem seems to be related to trying to bind my custom class TileServiceto mine MainActivity- whenever I bind this when the tile goes into unchanged state. I can’t understand why, though, because it is successfully connected. If I do not bind it (i.e., it just has a one-way connection between the command control), the tile is active, and the application responds to its selection.
Ultimately, I get attached to the link to my custom class TileServicein order to be able to call the method toggleTileUI(). I do not want to use a singleton or static member variable as a solution to get a link to my service.
Here is what I have in mine AndroidManifest.xml:
<service
android:name=".ConnectionQuickSettingsService"
android:label="@string/quick_setting_tile_connect"
android:icon="@drawable/tile_icon"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
Here's mine ConnectionQuickSettingsService.java:
@TargetApi(24)
public class ConnectionQuickSettingsService extends TileService {
private static final String TAG = "ConnectionQuickSettingsService";
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
ConnectionQuickSettingsService getService() {
return ConnectionQuickSettingsService.this;
}
}
@Override
public IBinder onBind(Intent i) {
return mBinder;
}
@Override
public void onTileAdded() {
L.d(TAG, "onTileAdded()");
}
@Override
public void onStartListening() {
L.d(TAG, "onStartListening()");
}
@Override
public void onClick() {
L.d(TAG, "Quick Settings tile selected");
toggleInAppSwitch();
toggleTileUI();
}
private void toggleInAppSwitch() {
doStuff();
}
public void toggleTileUI() {
Tile tile = this.getQsTile();
doStuffWithTile();
}
}
and finally (corresponding parts) my MainActivity.java:
@Override
protected void onStart() {
super.onStart();
Intent i = new Intent(this, ConnectionQuickSettingsService.class);
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
ConnectionQuickSettingsService.LocalBinder binder = (ConnectionQuickSettingsService.LocalBinder) service;
mQSService = binder.getService();
mBound = true;
L.d(TAG, "Bound to QS service successfully");
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
L.d(TAG, "Disconnected from QS service");
}
};
public void onOnOff(){
L.d(TAG, "On/Off switch toggled");
if (mBound) {
mQSService.toggleTileUI();
}
}
Any input would be appreciated, thanks!