Sunday, July 29, 2012

Android: Playing with System Settings

Probably if you are developing an Android Application, there will be numerous situations which requires you to modify some of the default settings of the Android system. But how to do that? Therefore...

I have created a simple app that will allow you to modify the System settings.
The above are some of the screen captures of the Android Application.

So here comes the source files...
AndroidManifest.xml (Take note of the user permissions.)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zcs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SimpleSystemSettingActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The main layout file - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <CheckBox
        android:id="@+id/chkBoxBrightness"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/chkBox" />

    <SeekBar
        android:id="@+id/seekBarBrightness"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:max="255" />

</LinearLayout>

The main application file - SimpleSystemSettingActivity.java
package com.zcs;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;

public class SimpleSystemSettingActivity extends Activity {
 private SeekBar seekBarBrightness;
 private CheckBox chkBoxBrightness;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Grab the components from the main view
        //and assign them to the respective variables
        seekBarBrightness = 
          (SeekBar)findViewById(R.id.seekBarBrightness);
        chkBoxBrightness = 
          (CheckBox)findViewById(R.id.chkBoxBrightness);
        
        //Adding the value change listeners for the
        //components
        seekBarBrightness.setOnSeekBarChangeListener(
          new seekBarChangedListener());
        chkBoxBrightness.setOnCheckedChangeListener(
          new checkBoxChangeListener());
        
        //Call the function updateSettings()
        updateSettings();
    }
    
    //This function will be used to update/retrieve the
    //values of the Screen Brightness and Screen Brightness
    //mode and assign the values to the respective components
    private void updateSettings()
    {
     try {
      //Grabbing the values of the Screen Brightness and 
      //Screen Brightness mode
   int screenBrightnessMode = Settings.System.getInt(
     getContentResolver(), 
     Settings.System.SCREEN_BRIGHTNESS_MODE);
   int screenBrightness = Settings.System.getInt(
     getContentResolver(), 
     Settings.System.SCREEN_BRIGHTNESS);
   
   seekBarBrightness.setProgress(screenBrightness);
   //If the Screen Brightness Mode is Automatic
   if(screenBrightnessMode == 
     Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
   {
    //we will hide the seek bar and check the 
    //check box
    chkBoxBrightness.setChecked(true);
    seekBarBrightness.setVisibility(View.INVISIBLE);
   }else{
    //else we will show it and uncheck the
    //check box
    chkBoxBrightness.setChecked(false);
    seekBarBrightness.setVisibility(View.VISIBLE);
   }
  } catch (SettingNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }
    
    //Change Listener for the seekbar for brightness
    private class seekBarChangedListener implements 
     SeekBar.OnSeekBarChangeListener {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
         int currentProgress = progress;
         
         //set the screen brightness into the System
         Settings.System.putInt(getContentResolver(), 
           Settings.System.SCREEN_BRIGHTNESS, 
           currentProgress);
         updateSettings();
        }
        public void onStartTrackingTouch(SeekBar seekBar) {}
        public void onStopTrackingTouch(SeekBar seekBar) {}
    }

    //Change Listener for the check bos for brightness mode
    private class checkBoxChangeListener implements 
     CheckBox.OnCheckedChangeListener
    {
  @Override
  public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
   boolean checkValue = isChecked;
   int intCheckValue = (checkValue)?1:0;
         //set the screen brightness mode into the System
   if(intCheckValue == 
     Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
   {
          Settings.System.putInt(getContentResolver(), 
            Settings.System.SCREEN_BRIGHTNESS_MODE, 
            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
   }else{
          Settings.System.putInt(getContentResolver(), 
            Settings.System.SCREEN_BRIGHTNESS_MODE, 
            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);    
   }
         updateSettings();
  }
    }
}

* Click here for the source files of this demo Android Application.
^ Click here for the complete list of System Settings that you can play around.

No comments:

Post a Comment