1. 由于在frameworks下的Systemui工程下,所以不需要所谓的反射调用(不懂)。
2. ui界面直接在工程下的status_bar_expanded.xml文件下修改ExpandedView组件添加,添加了10个按钮以实现10个开关功能。
添加的部分代码:
<HorizontalScrollView
android:id="@+id/Hscroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="86.0dip"
android:background="@drawable/appwidget_bg_holo"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/btn_wifi"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@drawable/appwidget_button_left"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_wifi"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_wifi"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_wifi"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_wifi"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<LinearLayout
android:id="@+id/btn_bluetooth"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_center"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_bluetooth"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_bluetooth"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_bluetooth"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_bluetooth"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<LinearLayout
android:id="@+id/btn_gps"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_center"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_gps"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_gps"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_gps"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_gps"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<LinearLayout
android:id="@+id/btn_sync"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_center"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_sync"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_sync"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_sync"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_sync"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<LinearLayout
android:id="@+id/btn_brightness"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_brightness"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_brightness"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_brightness"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_brightness"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<!-- xhh add ledflash 2013-4-15 -->
<LinearLayout
android:id="@+id/btn_ledflash"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_ledflash"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_ledflash"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_ledflash"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_ledflash"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<!-- xhh add airplane 2013-4-15 -->
<LinearLayout
android:id="@+id/btn_airplane"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_airplane"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_airplane"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_airplane"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_airplane"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<!-- xhh add data_connection 2013-4-15 -->
<LinearLayout
android:id="@+id/btn_data_connection"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_data_connection"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_data_connection"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_data_connection"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_data_connection"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<!-- xhh add rotation 2013-4-15 -->
<LinearLayout
android:id="@+id/btn_rotation"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_rotation"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_rotation"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_rotation"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_rotation"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
<ImageView
android:layout_width="1.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_settings_divider_holo" />
<!-- xhh add silent_mode 2013-4-15 -->
<LinearLayout
android:id="@+id/btn_silent_mode"
android:layout_width="63.0dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_button_right"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingTop="12.0dip" >
<ImageView
android:id="@+id/img_silent_mode"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="11.0"
android:scaleType="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="3.0" />
<TextView
android:id="@+id/status_bar_expanded_text_silent_mode"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="8.0"
android:gravity="center"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:text="@string/status_bar_expanded_text_silent_mode_sound"
android:textColor="@color/white"
android:textSize="12.0sp" />
<ImageView
android:id="@+id/ind_silent_mode"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
3.修改PhoneStatusBar.java
①在makeStatusBarView()函数修改:
ExpandedView expanded = (ExpandedView) View.inflate(context,
R.layout.status_bar_expanded, null);
得到我们刚刚画的view后,通过findViewsById()设置组件行为。
②在makeExpandedVisible()中添加刷新view的方法。
3.wifi开关的实现。
方法一:
①查询wifi状态
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
int test = wifiStateToFiveState(wifiManager.getWifiState());
private int wifiStateToFiveState(int wifiState) {
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
return STATE_DISABLED;
case WifiManager.WIFI_STATE_ENABLED:
return STATE_ENABLED;
case WifiManager.WIFI_STATE_DISABLING:
return STATE_TURNING_OFF;
case WifiManager.WIFI_STATE_ENABLING:
return STATE_TURNING_ON;
default:
return STATE_UNKNOWN;
}
}
②设置wifi开关:
wifiManager.setWifiEnabled(desiredState);
完整子函数,由于wifi的开启关闭需要较长时间,需要开启一个线程来执行。
protected void requestStateChange(Context context,
final boolean desiredState) {
final WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
Log.d(TAG, "No wifiManager.");
return;
}
// Actually request the wifi change and persistent
// settings write off the UI thread, as it can take a
// user-noticeable amount of time, especially if there's
// disk contention.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... args) {
/**
* Disable tethering if enabling Wifi
*/
if (debug) {
Log.i(TAG, "wifi requestStateChange doInBackground 11");
}
int wifiApState = wifiManager.getWifiApState();
if (desiredState
&& ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING)
|| (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
wifiManager.setWifiApEnabled(null, false);
}
wifiManager.setWifiEnabled(desiredState);
return null;
}
@Override
protected void onPostExecute(Void result) {
mInTransition = false;
}
sWifiState.setImageViewResources(mContext, LAYOUT_WIFI);
}
}.execute();
}
方法二(WIFI未证实):
通过设置android.provider的setting修改设置,达到设置wifi开关,同时只要找到相应的provider string值就能修改setting里的设置。
Settings.System.putInt(context.getContentResolver(),
Settings.System.WIFI_ON, enableAM ? 1 : 0);
status = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.WIFI_ON);
4.蓝牙实现。
方法一:蓝牙的实现比较复杂,需要从setting工程里面移植相应的蓝牙packge.
public int getActualState(Context context) {
if (sLocalBluetoothAdapter == null) {
LocalBluetoothManager manager = LocalBluetoothManager
.getInstance(context);
if (manager == null) {
return STATE_UNKNOWN; // On emulator?
}
sLocalBluetoothAdapter = manager.getBluetoothAdapter();
}
int test = bluetoothStateToFiveState(sLocalBluetoothAdapter
.getBluetoothState());
if (test == STATE_TURNING_ON || test == STATE_TURNING_OFF) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... args) {
try {
Thread.sleep(4000);
} catch (Exception e) {
e.printStackTrace();
}
;
return null;
}
@Override
protected void onPostExecute(Void result) {
sBluetoothState.setImageViewResources(mContext,
LAYOUT_BLUETOOTH);
}
}.execute();
}
return test;
}
@Override
protected void requestStateChange(Context context,
final boolean desiredState) {
if (sLocalBluetoothAdapter == null) {
Log.d(TAG, "No LocalBluetoothManager");
return;
}
// Actually request the Bluetooth change and persistent
// settings write off the UI thread, as it can take a
// user-noticeable amount of time, especially if there's
// disk contention.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... args) {
sLocalBluetoothAdapter.setBluetoothEnabled(desiredState);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
;
return null;
}
@Override
protected void onPostExecute(Void result) {
mInTransition = false;
sBluetoothState.setImageViewResources(mContext,
LAYOUT_BLUETOOTH);
}
}.execute();
}
方法二:同样通过android.provider.settings来实现,string值为Settings.System.BLUETOOTH_ON。
5.GPS开关。
public int getActualState(Context context) {
ContentResolver resolver = context.getContentResolver();
boolean on = Settings.Secure.isLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER);
return on ? STATE_ENABLED : STATE_DISABLED;
}
@Override
public void requestStateChange(final Context context,
final boolean desiredState) {
final ContentResolver resolver = context.getContentResolver();
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... args) {
Settings.Secure.setLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER, desiredState);
return desiredState;
}
@Override
protected void onPostExecute(Boolean result) {
setCurrentState(context, result ? STATE_ENABLED
: STATE_DISABLED);
}
updateButtons(context);
}
}.execute();
}
6.同步实现。
public int getActualState(Context context) {
boolean on = ContentResolver.getMasterSyncAutomatically();
return on ? STATE_ENABLED : STATE_DISABLED;
}
public void requestStateChange(final Context context,
final boolean desiredState) {
final ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean sync = ContentResolver.getMasterSyncAutomatically();
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... args) {
// Turning sync on.
if (desiredState) {
if (!sync) {
ContentResolver.setMasterSyncAutomatically(true);
}
return true;
}
// Turning sync off
if (sync) {
ContentResolver.setMasterSyncAutomatically(false);
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
setCurrentState(context, result ? STATE_ENABLED
: STATE_DISABLED);
updateButtons(context);
}
}.execute();
}
7.背光 三档控制
/**
* Minimum and maximum brightnesses. Don't go to 0 since that makes the
* display unusable
*/
private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;
private static final int DEFAULT_BACKLIGHT = (int) (android.os.Power.BRIGHTNESS_ON * 0.4f);
/** Minimum brightness at which the indicator is shown at half-full and ON */
private static final int HALF_BRIGHTNESS_THRESHOLD = (int) (0.3 * MAXIMUM_BACKLIGHT);
/** Minimum brightness at which the indicator is shown at full */
private static final int FULL_BRIGHTNESS_THRESHOLD = (int) (0.8 * MAXIMUM_BACKLIGHT);
private int getBrightness(Context context) {
try {
IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
.getService("power"));
if (power != null) {
int brightness = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
return brightness;
}
} catch (Exception e) {
}
return 0;
}
/**
* Gets state of brightness mode.
*
* @param context
* @return true if auto brightness is on.
*/
private boolean getBrightnessMode(Context context) {
try {
IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
.getService("power"));
if (power != null) {
int brightnessMode = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
return brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
}
} catch (Exception e) {
Log.d(TAG, "getBrightnessMode: " + e);
}
return false;
}
/**
* Increases or decreases the brightness.
*
* @param context
*/
private void toggleBrightness(Context context) {
try {
IPowerManager power = IPowerManager.Stub.asInterface(ServiceManager
.getService("power"));
if (power != null) {
ContentResolver cr = context.getContentResolver();
int brightness = Settings.System.getInt(cr,
Settings.System.SCREEN_BRIGHTNESS);
int brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
// Only get brightness setting if available
if (context
.getResources()
.getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available)) {
brightnessMode = Settings.System.getInt(cr,
Settings.System.SCREEN_BRIGHTNESS_MODE);
}
// Rotate AUTO -> MINIMUM -> DEFAULT -> MAXIMUM
// Technically, not a toggle...
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
brightness = MINIMUM_BACKLIGHT;
brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
} else if (brightness < DEFAULT_BACKLIGHT) {
brightness = DEFAULT_BACKLIGHT;
} else if (brightness < MAXIMUM_BACKLIGHT) {
brightness = MAXIMUM_BACKLIGHT;
} else {
brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
brightness = MINIMUM_BACKLIGHT;
}
if (context
.getResources()
.getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available)) {
// Set screen brightness mode (automatic or manual)
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
brightnessMode);
} else {
// Make sure we set the brightness if automatic mode isn't
// available
brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
}
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) {
power.setBacklightBrightness(brightness);
Settings.System.putInt(cr,
Settings.System.SCREEN_BRIGHTNESS, brightness);
}
}
} catch (RemoteException e) {
Log.d(TAG, "toggleBrightness: " + e);
} catch (Settings.SettingNotFoundException e) {
Log.d(TAG, "toggleBrightness: " + e);
}
}
/** Observer to watch for changes to the BRIGHTNESS setting */
private class SettingsObserver extends ContentObserver {
private Context mContext;
SettingsObserver(Handler handler, Context context) {
super(handler);
mContext = context;
}
void startObserving() {
ContentResolver resolver = mContext.getContentResolver();
// Listen to brightness and brightness mode
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.SCREEN_BRIGHTNESS), false, this);
resolver.registerContentObserver(Settings.System
.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE), false,
this);
}
void stopObserving() {
mContext.getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange) {
updateBrightness(mContext);
}
}
8.手电筒 实现
public class LedFlashButton {
private String TAG = "LedFlashButton";
private boolean DBG = true;
public static final byte[] LIGHTE_ON = { '2', '5', '5' };
public static final byte[] LIGHTE_OFF = { '0' };
private static Camera mCamera = null;
private static Parameters mCameraParas = null;
private Context mContext = null;
private View mView = null;
private ImageView bt = null;
private ImageView in = null;
private LinearLayout led = null;
private static boolean mflashOn = false;
private int[] ind = { R.drawable.appwidget_settings_ind_on_holo,
R.drawable.appwidget_settings_ind_off_holo };
private int[] img = { R.drawable.tw_quick_panel_icon_flashlight_on,
R.drawable.tw_quick_panel_icon_flashlight_dim,
R.drawable.tw_quick_panel_icon_flashlight_off };
public LedFlashButton(Context mContext, View mView) {
super();
this.mContext = mContext;
this.mView = mView;
initLEDbutton();
setLEDStatus(mflashOn);
updateFlashButton();
}
private void initLEDbutton() {
bt = (ImageView) mView.findViewById(R.id.img_ledflash);
in = (ImageView) mView.findViewById(R.id.ind_ledflash);
led = (LinearLayout) mView.findViewById(R.id.btn_ledflash);
led.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mflashOn = !mflashOn;
setLEDStatus(mflashOn);
updateFlashButton();
}
});
}
private void setLEDStatus(boolean status) {
FileOutputStream red;
try {
if (DBG)
Log.d(TAG, "current LED status " + status);
byte[] ledData = status ? LIGHTE_ON : LIGHTE_OFF;
red = new FileOutputStream("/sys/class/leds/flashlight/brightness");
red.write(ledData);
red.close();
} catch (FileNotFoundException e) {
Log.d(TAG, e.toString());
if (null == mCamera) {
mCamera = Camera.open();
}
mCameraParas = mCamera.getParameters();
if (status) {
mCameraParas.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(mCameraParas);
} else {
mCameraParas.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(mCameraParas);
}
// using another method
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateFlashButton() {
if (mflashOn) {
bt.setImageResource(img[0]);
in.setImageResource(ind[0]);
} else {
bt.setImageResource(img[2]);
in.setImageResource(ind[1]);
}
}
}
9.屏幕旋转
private ImageView rotation_im = null;
private ImageView rotation_in = null;
private LinearLayout rotation_bt = null;
private int[] rotation_img = { R.drawable.tw_quick_panel_icon_rotation_on,
R.drawable.tw_quick_panel_icon_rotation_off };
private final int ROTATION_STATUS_ENABLE = 1;
private final int ROTATION_STATUS_UNABLE = 0;
private final int ROTATION_STATUS_UNKNOW = 2;
private int getRotationStatus() {
int status = 5;
try {
status = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "SettingNotFoundException");
}
if (status == 5) {
return ROTATION_STATUS_UNKNOW;
} else {
return status;
}
}
private void setRotation(Context context, boolean enable) {
// set the airplane mode
Settings.System.putInt(context.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, enable ? 1 : 0);
// if API level>=17
// Settings.Global.putInt(context.getContentResolver(),
// Settings.Global.ACCELEROMETER_ROTATION, enableAM ? 1 : 0);
}
private void updatRotationButtons() {
switch (getRotationStatus()) {
case ROTATION_STATUS_ENABLE:
rotation_im.setImageResource(rotation_img[0]);
rotation_in.setImageResource(ind[0]);
break;
case ROTATION_STATUS_UNABLE:
rotation_im.setImageResource(rotation_img[1]);
rotation_in.setImageResource(ind[1]);
break;
default:
rotation_im.setImageResource(rotation_img[0]);
rotation_in.setImageResource(ind[1]);
}
}
private void rotationClick() {
switch (getRotationStatus()) {
case ROTATION_STATUS_ENABLE:
setRotation(mContext, false);
break;
case ROTATION_STATUS_UNABLE:
setRotation(mContext, true);
break;
default:
Toast.makeText(mContext, "wait..", Toast.LENGTH_LONG).show();
}
updatRotationButtons();
}
10.飞行模式 airplane
private ImageView airplane_im = null;
private ImageView airplane_in = null;
private LinearLayout airplane_bt = null;
private int[] airplane_img = { R.drawable.tw_quick_panel_icon_airplane_on,
R.drawable.tw_quick_panel_icon_airplane_dim,
R.drawable.tw_quick_panel_icon_airplane_off };
private final int AIRPLANE_STATUS_ENABLE = 1;
private final int AIRPLANE_STATUS_UNABLE = 0;
private final int AIRPLANE_STATUS_UNKNOW = 2;
private void airplaneClick() {
switch (getAirplaneModeStatus()) {
case AIRPLANE_STATUS_ENABLE:
setAirplaneMode(mContext, false);
break;
case AIRPLANE_STATUS_UNABLE:
setAirplaneMode(mContext, true);
break;
default:
Toast.makeText(mContext, "wait..", Toast.LENGTH_LONG).show();
}
updateAirplaneButtons();
}
/**
* Set airplane mode
*/
private void setAirplaneMode(Context context, boolean enableAM) {
// set the airplane mode
Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, enableAM ? 1 : 0);
// if API level>=17
// Settings.Global.putInt(context.getContentResolver(),
// Settings.Global.AIRPLANE_MODE_ON, enableAM ? 1 : 0);
// Post the intent
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", enableAM);
context.sendBroadcast(intent);
}
private int getAirplaneModeStatus() {
int status = 5;
try {
status = Settings.System.getInt(mContext.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "SettingNotFoundException");
}
// if (DBG) {
// // Log.i(TAG, "status=" + String.valueOf(status));
// }
switch (status) {
case 1:
return AIRPLANE_STATUS_ENABLE;
case 0:
return AIRPLANE_STATUS_UNABLE;
default:
return AIRPLANE_STATUS_UNKNOW;
}
}
private void updateAirplaneButtons() {
switch (getAirplaneModeStatus()) {
case AIRPLANE_STATUS_ENABLE:
airplane_im.setImageResource(airplane_img[0]);
airplane_in.setImageResource(ind[0]);
break;
case AIRPLANE_STATUS_UNABLE:
airplane_im.setImageResource(airplane_img[2]);
airplane_in.setImageResource(ind[1]);
break;
default:
airplane_im.setImageResource(airplane_img[1]);
airplane_in.setImageResource(ind[1]);
// Toast.makeText(mContext, "AIRPLANE_STATUS_UNKNOW",
// Toast.LENGTH_LONG).show();
}
}
11.静音模式 silent mode
private ImageView sound_im = null;
private ImageView sound_in = null;
private LinearLayout sound_bt = null;
private TextView sound_text = null;
private int[] sound_img = {
R.drawable.tw_quick_panel_icon_silent_mode_on_on,
R.drawable.tw_quick_panel_icon_silent_mode_on_off,
R.drawable.tw_quick_panel_icon_silent_on,
R.drawable.tw_quick_panel_icon_silent_off };
private static AudioManager mAudioManager = null;
private static Vibrator mVibrator = null;
private final int VIBRATE_TIME = 300;
private void SilentClick() {
switch (getSilentModeStatus()) {
case AudioManager.RINGER_MODE_NORMAL:
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case AudioManager.RINGER_MODE_SILENT:
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case AudioManager.RINGER_MODE_VIBRATE:
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
break;
default:
Toast.makeText(mContext, "AudioManager not found",
Toast.LENGTH_LONG).show();
}
updateSilentButtons();
}
private int getSilentModeStatus() {
if (mAudioManager == null) {
Log.i(TAG, "mAudioManager not found");
return 5;
}
return mAudioManager.getRingerMode();
}
private void updateSilentButtons() {
switch (getSilentModeStatus()) {
case AudioManager.RINGER_MODE_NORMAL:
sound_im.setImageResource(sound_img[3]);
sound_in.setImageResource(ind[1]);
sound_text
.setText(R.string.status_bar_expanded_text_silent_mode_sound);
break;
case AudioManager.RINGER_MODE_SILENT:
sound_im.setImageResource(sound_img[2]);
sound_in.setImageResource(ind[0]);
sound_text
.setText(R.string.status_bar_expanded_text_silent_mode_mute);
break;
case AudioManager.RINGER_MODE_VIBRATE:
sound_im.setImageResource(sound_img[0]);
sound_in.setImageResource(ind[0]);
sound_text
.setText(R.string.status_bar_expanded_text_silent_mode_vibrate);
mVibrator.vibrate(VIBRATE_TIME);
break;
default:
Toast.makeText(mContext, "AudioManager not found",
Toast.LENGTH_LONG).show();
}
}
12.移动数据 GPRS上网
方法一: 这个方法能够实现开关作用,但是设置菜单里面对应的设置没有办法实现自动更新。
设置:
ConnectivityManager cm = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.setMobileDataEnabled(flag);
查询:
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (null == networkInfo)
{
Toast.makeText(mContext, "网络没有连接!", Toast.LENGTH_SHORT).show();
}
或者
private void setDataConnection(boolean desire) {
if (cm != null) {
cm.setMobileDataEnabled(desire);
}
}
private boolean getDataConnectionStatus() {
if (cm != null) {
return cm.getMobileDataEnabled();
}
return false;
}
private void updateDataButton() {
if (getDataConnectionStatus()) {
Log.i(TAG, "updateDataButton true");
data_im.setImageResource(data_img[0]);
data_in.setImageResource(ind[0]);
} else {
Log.i(TAG, "updateDataButton false");
data_im.setImageResource(data_img[2]);
data_in.setImageResource(ind[1]);
}
}
private void dataClick(){
if (getDataConnectionStatus()){
setDataConnection(false);
}else {
setDataConnection(true);
}
data_im.setImageResource(data_img[1]);
data_in.setImageResource(ind[0]);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateDataButton();
}}, 200);
}