Skip to content

Instantly share code, notes, and snippets.

@jerrellmardis
Created June 3, 2016 22:10
Show Gist options
  • Save jerrellmardis/e990b2ff6a5e349e5041ad4d13639af1 to your computer and use it in GitHub Desktop.
Save jerrellmardis/e990b2ff6a5e349e5041ad4d13639af1 to your computer and use it in GitHub Desktop.
A quick example demonstrating how to create a Quick Settings Tile using the new Quick Settings Tile API introduced in Android N.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.jerrellmardis.quicksettings.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
import android.provider.Settings;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
/**
* <p>
* A {@link TileService} which toggles the user's display timeout.
* </p>
*/
public class DisplayTimeoutTileService extends TileService {
public DisplayTimeoutTileService() {
}
@Override
public void onStartListening() {
Tile tile = getQsTile();
if (tile != null) {
tile.setState(Tile.STATE_ACTIVE);
tile.updateTile();
}
super.onStartListening();
}
@Override
public void onClick() {
try {
int time = Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT);
final long twoMinutes = TimeUnit.MINUTES.toMillis(2);
// toggle display timeout
if (time == twoMinutes) {
Settings.System.putLong(getContentResolver(), SCREEN_OFF_TIMEOUT, TimeUnit.MINUTES.toMillis(30));
Toast.makeText(getApplicationContext(), getString(R.string.timeout_display_msg_30), Toast.LENGTH_SHORT).show();
} else {
Settings.System.putLong(getContentResolver(), SCREEN_OFF_TIMEOUT, TimeUnit.MINUTES.toMillis(2));
Toast.makeText(getApplicationContext(), getString(R.string.timeout_display_msg_2), Toast.LENGTH_SHORT).show();
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
}
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
// you need to allow the app to modify System settings
// this is only for testing purposes
// you should explain to the user why they are being directed
// to the settings screen before starting the new activity
if (!Settings.System.canWrite(this)) {
startActivity(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS));
}
}
}
<resources>
<string name="app_name">Quick Settings</string>
<string name="action_settings">Settings</string>
<string name="display_timeout">Display Timeout</string>
<string name="timeout_display_msg_30">"Timeout set to 30 minutes"</string>
<string name="timeout_display_msg_2">"Timeout set to 2 minutes"</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment