http://www.eoeandroid.com/forum-redirect-tid-66701-goto-lastpost.html#lastpost
看到论坛上有很多问亮度的问题(只能改变当前的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 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>
Android手电筒核心代码
/**
* true电筒打开
* false电筒关闭
*/
private boolean isOpen = false;
/**
* 打开闪光灯
*/
private void openFlashLight() {
if (!isOpen) {//闪光灯处于关闭状态
if (camera==null) {
camera=Camera.open();
}
camera.startPreview();
Parameters parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
isOpen = true;
}
}
/**
* 关闭闪光灯
*/
private void closeFlashLight() {
if (isOpen) {//闪光灯已经打开
if (camera != null) {
Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
}
isOpen = false;
}
}