Android Brightness Changing

來源:互聯網
上載者:User

這內容挺用的,特別是需要調節亮度的程式,自己收藏起來,呵呵。這段代碼不只能改變當前的Activity,也可以改變整個System的亮度

代碼:

package com.jimmy;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MyActivity extends Activity {
        /** Called when the activity is first created. */
        TextView textView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                textView = (TextView) findViewById(R.id.MyTextView);
                updateToggles();
        }

        private void updateToggles() {
                // TODO Auto-generated method stub
                SeekBar seekBar = (SeekBar) findViewById(R.id.MySeekBar);
                seekBar.setProgress((int) (android.provider.Settings.System.getInt(
                                getContentResolver(),
                                android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) ));
                seekBar.setOnSeekBarChangeListener(seekListener);
        }

        private OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() {

                public void onProgressChanged(SeekBar seekBar, int progress,
                                boolean fromUser) {
                        if (fromUser) {
                                Integer tmpInt = seekBar.getProgress();
                                System.out.println(tmpInt);
                                // 51 (seek scale) * 5 = 255 (max brightness)
                                // Old way
                                android.provider.Settings.System.putInt(getContentResolver(),
                                                android.provider.Settings.System.SCREEN_BRIGHTNESS,
                                                tmpInt); // 0-255
                                tmpInt = Settings.System.getInt(getContentResolver(),
                                                Settings.System.SCREEN_BRIGHTNESS, -1);
                                // Cupcake way..... sucks
                                WindowManager.LayoutParams lp = getWindow().getAttributes();
                                // lp.screenBrightness = 1.0f;
                                // Float tmpFloat = (float)tmpInt / 255;
                                if (0<= tmpInt && tmpInt <= 255) {
                                        lp.screenBrightness = tmpInt;
                                }
                               
                               
                                getWindow().setAttributes(lp);
                        }

                }

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

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                        // and here too
                }
        };
}這是主程式

<xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="@color/white">
        <TextView android:id="@+id/MyTextView"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:text="亮度是:" />
       
                <SeekBar
                android:layout_gravity="center_horizontal"
                android:id="@+id/MySeekBar"
                android:paddingLeft="5.0dip"
                android:paddingRight="5.0dip"
                android:layout_width="fill_parent"
                android:layout_height="150dip"
                android:layout_marginTop="10.0dip"
                android:layout_marginBottom="10.0dip"
                android:max="255"
                >
        </SeekBar>
</LinearLayout>
下面是許可權
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.jimmy" android:versionCode="1" android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
                <activity android:name=".MyActivity" 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>
        <uses-sdk android:minSdkVersion="5" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
        <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
        <uses-permission android:name="android.permission.DEVICE_POWER"></uses-permission>
        <uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>

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

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

</manifest>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.