How To change System Settings in Android ?

How To change System Settings in android ?
OR

How to make System Changes ?
OR
AndroProgrammer.comHow To Change Brightness from my application ? etc...

To make System application or part of it you have to Change certain parameters in Android Settings. Let’s say somehow you have to make some changes in system configuration like display brightness or Ring tone volume from your application. So here is solution require for your application or Part of it. There are number of application in the market who have used these things in application (Tasker  etc.). so now you can also integrate it in your application also or using it you can create some awesome apps.



To make Changes in settings I have used android.provider.setting class of android freamwork. You can check out developer document for further reference.
In Android Operating system there are number of Settings but I am gone cover simple setting changes in this tutorial so let’s start with its pre-requisite.

Steps
  1. Create New Application and give name it System Settings and Do next. 
    1. minSdkVersion="17” 
    2. targetSdkVersion="17". 
  2. Give activity name – Main.java and main.xml. 
  3. Go to main.xml and post the below code it contains, 
    1. Two Seek bar for brightness and volume. 
    2. Two Buttons and Two Text view. (Check below screen recording for further layout.)

activity_systemsetting_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="?attr/actionBarSize"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:text="Brightness"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/Sbbrihgtness"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="35dp"
        android:max="255"/>

    <SeekBar
        android:id="@+id/Sbvolume"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="35dp"
        android:max="255" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/Sbbrihgtness"
        android:text="Set Brightness" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/Sbvolume"
        android:layout_marginTop="24dp"
        android:text="Set Volume" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="41dp"
        android:text="Volume"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Now let’s start some coding. I have created method for setting references and on click listener method for 2 buttons. In order to get seek bar value we having onChangeListner method of seekbar object. It consist 3 methods that you have to over ride.
  1. onStartTrackingTouch.
  2. onStopTrackingTouch .
  3. onProgressChanged.

We can get Seek bar value In onProgressChanged method. Now Paste below code in Main.java class file and make sure you put code as it is.


SystemSettingDemo.java
package com.androprogrammer.tutorials.samples;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;

import com.androprogrammer.tutorials.R;
import com.androprogrammer.tutorials.activities.Baseactivity;

public class SystemSettingDemo extends Baseactivity implements View.OnClickListener {

    private View view;

    protected Button Bbright,Bvolume;
    protected SeekBar brightness,volume;
    protected int set_brightness,set_volume;
    protected float value_brightness,value_volume;

    private AudioManager audioManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setReference();

        setToolbarElevation(7);

        setToolbarSubTittle(this.getClass().getSimpleName());

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Bbright.setOnClickListener(this);
        Bvolume.setOnClickListener(this);

        try {
            set_brightness = android.provider.Settings.System.getInt(getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS);

            brightness.setProgress(set_brightness);
        } catch (Settings.SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub
                set_brightness = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }
        });

        volume.setMax(audioManager
                .getStreamMaxVolume(AudioManager.STREAM_RING));
        volume.setProgress(audioManager
                .getStreamVolume(AudioManager.STREAM_RING));

        volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                // TODO Auto-generated method stub
                set_volume = progress;
            }
        });


    }

    @Override
    public void setReference() {

        view = LayoutInflater.from(this).inflate(R.layout.activity_systemsetting_demo, container);

        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        brightness = (SeekBar) view.findViewById(R.id.Sbbrihgtness);
        volume= (SeekBar) view.findViewById(R.id.Sbvolume);
        Bbright = (Button) view.findViewById(R.id.button1);
        Bvolume = (Button) view.findViewById(R.id.button2);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button1:
                android.provider.Settings.System.putInt(getContentResolver(),
                        android.provider.Settings.System.SCREEN_BRIGHTNESS, set_brightness);
                break;
            case R.id.button2:
                audioManager.setStreamVolume(AudioManager.STREAM_RING, set_volume, 0);

                break;
            default:
                break;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
To make this changes in settings you have to use certain permissions. So go to mainefest file and add these permissions.

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />


Screen Shot
Androprogrammer.com


So that’s it. Now run your application and see how it works. I hope you like my method and if you have certain thoughts or comments then post in below comment section.

Keep coding…

0 comments :

Post a Comment