我们在市场上经常可以看到,可以切换蓝牙,wifi, 飞行模式等等功能的小部件,由于工作需要,我也把相应的功能实现了一下
有需要的朋友,避免走弯路,可以参考下面代码,也可以到
http://download.csdn.net/detail/c_weibin/4142010
下载完整项目
这里直接将源码贴出
package com.bpi.launcher.view; import android.app.Activity; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.media.AudioManager; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Bundle; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import android.widget.RelativeLayout.LayoutParams; import com.bpi.launcher.R; public class ToggleToolWidget extends LinearLayout implements OnClickListener{ /** Called when the activity is first created. */ protected ImageView iv_bluetooth = null;//蓝牙 protected ImageView iv_wifi = null;//wifi protected ImageView iv_sound = null;//声音 protected ImageView iv_brightness = null;//亮度 protected ImageView iv_flymodel = null;//飞行模式 protected ImageView iv_gps = null;//GPS protected BluetoothAdapter adapter = null; protected WifiManager wifiManager = null; protected AudioManager audioManager = null; protected LocationManager locationManager=null; protected final static int mostBrightness=255;//最亮 protected final static int moreBrightness=150;//较亮 protected final static int lowBrightness=50;//最暗 View linearLayout=null; public ToggleToolWidget(Context context) { super(context); LayoutInflater inflater=LayoutInflater.from(getContext()); View linearLayout=inflater.inflate(R.layout.toggle_tool, null); this.addView(linearLayout); this.setGravity(LinearLayout.HORIZONTAL|LinearLayout.VERTICAL); //this.setLayoutParams(layoutParams); /**初始化有关蓝牙的信息**/ adapter = BluetoothAdapter.getDefaultAdapter(); iv_bluetooth = (ImageView) findViewById(R.id.bluetooth);//蓝牙 iv_bluetooth.setOnClickListener(this); IntentFilter intentFilter = new IntentFilter( android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED); getContext().registerReceiver(new BluetoothBroadcastReceiver(), intentFilter);//注册监听器 changeBluetoothImage(); /**初始化有关wifi的信息**/ wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); iv_wifi = (ImageView) findViewById(R.id.wifi); iv_wifi.setOnClickListener(this); IntentFilter wifiIntentFilter = new IntentFilter( android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION); getContext().registerReceiver(new WifiBroadcastReceiver(), wifiIntentFilter); changeWifiImage(); /**初始化声音信息**/ audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); iv_sound = (ImageView) findViewById(R.id.sound); iv_sound.setOnClickListener(this); changeRingerModeImage(); IntentFilter soundIntentFilter = new IntentFilter( android.media.AudioManager.RINGER_MODE_CHANGED_ACTION); getContext().registerReceiver(new RingerModeBrocastReceiver(), soundIntentFilter); /**初使化亮度信息**/ iv_brightness = (ImageView) findViewById(R.id.brightness); iv_brightness.setOnClickListener(this); changeBrinessImage(); /**飞行模式**/ iv_flymodel = (ImageView) findViewById(R.id.flymodel); iv_flymodel.setOnClickListener(this); IntentFilter airPlaneIntentFilter = new IntentFilter( Intent.ACTION_AIRPLANE_MODE_CHANGED); getContext().registerReceiver(new AirPlaneBrocastReceiver(), airPlaneIntentFilter); changeAirPlaneImage(); /**初使化GpS信息**/ iv_gps = (ImageView) findViewById(R.id.gps); iv_gps.setOnClickListener(this); locationManager=(LocationManager) getContext().getSystemService(getContext().LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0.0F, new GPSListener()); if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){//初始化显示的图片 iv_gps.setImageResource(R.drawable.gps_on); }else{ iv_gps.setImageResource(R.drawable.gps_off); } } @Override public void onClick(View v) { if (v.getId() == R.id.bluetooth) {//点击蓝牙图标 if (adapter.getState() == BluetoothAdapter.STATE_OFF) { adapter.enable(); } else if (adapter.getState() == BluetoothAdapter.STATE_ON) { adapter.disable(); } } else if (v.getId() == R.id.wifi) {//点击wifi图标 wifiManager.setWifiEnabled(!wifiManager.isWifiEnabled()); } else if (v.getId() == R.id.sound) {//点击响铃方式 int ringerMode = audioManager.getRingerMode(); if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {//正常时点击后变振动 audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); //Toast.makeText(this, "振动", 0).show(); } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {//振动时点击后变静音 audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //Toast.makeText(this, "静音", 0).show(); } else if (ringerMode == AudioManager.RINGER_MODE_SILENT) {//静音时点击后变正常 audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); //Toast.makeText(this, "正常", 0).show(); } } else if (v.getId() == R.id.brightness) {//点击亮度 //设置当前屏幕的亮度 try { int currentBrightness=Settings.System.getInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS); currentBrightness=getNearestNumber(currentBrightness, mostBrightness,moreBrightness,lowBrightness); if(currentBrightness==mostBrightness){ currentBrightness=moreBrightness; iv_brightness.setImageResource(R.drawable.brightness_auto); }else if(currentBrightness==moreBrightness){ currentBrightness=lowBrightness; iv_brightness.setImageResource(R.drawable.brightness_low); }else { iv_brightness.setImageResource(R.drawable.brightness_most); currentBrightness=mostBrightness; } //设置当前Activity的亮度 WindowManager.LayoutParams lp = ((Activity)getContext()).getWindow().getAttributes(); lp.screenBrightness=currentBrightness/255f; ((Activity)getContext()).getWindow().setAttributes(lp); //设置整个手机的亮度 Settings.System.putInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, currentBrightness); } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } else if (v.getId() == R.id.flymodel) { //取得目前飞行模式的状态 boolean isAirPlaneOn; try { isAirPlaneOn = Settings.System.getInt(getContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON) == 1 ? true : false; setAirplaneMode(!isAirPlaneOn); } catch (SettingNotFoundException e) { e.printStackTrace(); } } else if (v.getId() == R.id.gps) { toggleGPS(); } } /** * 改变亮度图片 * * 陈伟斌 * 2012-2-18 */ protected void changeBrinessImage(){ int currentBrightness; try { currentBrightness = Settings.System.getInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS); currentBrightness=getNearestNumber(currentBrightness, mostBrightness,moreBrightness,lowBrightness); if(currentBrightness==mostBrightness){ iv_brightness.setImageResource(R.drawable.brightness_most); }else if(currentBrightness==moreBrightness){ iv_brightness.setImageResource(R.drawable.brightness_auto); }else { iv_brightness.setImageResource(R.drawable.brightness_low); } } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 改变蓝牙切换按钮的图片 * 陈伟斌 * 2012-2-9 */ protected void changeBluetoothImage() { int blueState = adapter.getState(); if (blueState == BluetoothAdapter.STATE_ON) {//打开蓝牙 iv_bluetooth.setImageResource(R.drawable.bluetooth_on); } else if (blueState == BluetoothAdapter.STATE_OFF) {//关闭蓝牙 iv_bluetooth.setImageResource(R.drawable.bluetooth_off); } else if (blueState == BluetoothAdapter.STATE_TURNING_OFF || blueState == BluetoothAdapter.STATE_TURNING_ON) {//正在打开蓝牙或正在关闭蓝牙 iv_bluetooth.setImageResource(R.drawable.bluetooth_ing); } } /** * 改变wifi切换按钮的图片 * * 陈伟斌 * 2012-2-9 */ protected void changeWifiImage() { int wifiState = wifiManager.getWifiState(); if (wifiState == WifiManager.WIFI_STATE_DISABLED) {//关闭 iv_wifi.setImageResource(R.drawable.wifi_disabled); } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {//可用 iv_wifi.setImageResource(R.drawable.wifi_enabled); } else if (wifiState == WifiManager.WIFI_STATE_DISABLING || wifiState == WifiManager.WIFI_STATE_ENABLING) {//正在打开wifi或正在关闭wifi iv_wifi.setImageResource(R.drawable.wifi_enabling); } } /** * 改变ringerMode切换按钮的图片 * * 陈伟斌 * 2012-2-9 */ protected void changeRingerModeImage() { int ringerMode = audioManager.getRingerMode(); if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {//正常 iv_sound.setImageResource(R.drawable.sound_on); } else if (ringerMode == AudioManager.RINGER_MODE_SILENT) {//静音 iv_sound.setImageResource(R.drawable.sound_off); } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {//振动 iv_sound.setImageResource(R.drawable.vibration); } } protected void changeAirPlaneImage() { try { boolean isAirPlaneOn = Settings.System.getInt(getContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON) == 1 ? true : false; if (isAirPlaneOn) { iv_flymodel.setImageResource(R.drawable.fly_model_on); } else { iv_flymodel.setImageResource(R.drawable.fly_model_off); } } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 传入若干个数,取得最接近destNumber的数 * @param numbers * @return * 陈伟斌 * 2012-2-10 */ private int getNearestNumber(int destNumber,int...numbers){ int diffValue=Integer.MAX_VALUE; int nearestNumber=numbers[0]; for (int i = 0; i < numbers.length; i++) { System.out.println(diffValue+" "+Math.abs(destNumber-numbers[i])); if(diffValue>Math.abs(destNumber-numbers[i])){ diffValue=Math.abs(destNumber-numbers[i]); nearestNumber=numbers[i]; } } return nearestNumber; } /** * 改变飞行模式 * @param setAirPlane * 陈伟斌 * 2012-2-10 */ private void setAirplaneMode(boolean setAirPlane) { Settings.System.putInt(getContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0); Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); intent.putExtra("state", setAirPlane);//平板没加这句话不起作用 getContext().sendBroadcast(intent); } /** * 切换GPS状态 * * 陈伟斌 * 2012-2-10 */ private void toggleGPS() { Intent gpsIntent = new Intent(); gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(getContext(), 0, gpsIntent, 0).send(); } catch (CanceledException e) { e.printStackTrace(); } } /** * * 项目名称:ToolWidget * 类名称: SettingBroadcastReceiver * 类描述:蓝牙广播接收器 * 创建人:陈伟斌 * 创建时间:2012-2-9 下午02:45:49 * */ class BluetoothBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (iv_bluetooth == null) { return; } changeBluetoothImage(); } } /** * * 项目名称:ToolWidget * 类名称: WifiBroadcastReceiver * 类描述:wifi广播接收器 * 创建人:陈伟斌 * 创建时间:2012-2-10 上午11:43:41 * */ class WifiBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { changeWifiImage(); } } /** * * 项目名称:ToolWidget * 类名称: RingerModeBrocastReceiver * 类描述:响铃模式 * 创建人:陈伟斌 * 创建时间:2012-2-10 上午11:44:00 * */ class RingerModeBrocastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { changeRingerModeImage(); } } /** * * 项目名称:ToolWidget * 类名称: AirPlaneBrocastReceiver * 类描述:飞行模式广播接收器 * 创建人:陈伟斌 * 创建时间:2012-2-10 上午11:45:00 * */ class AirPlaneBrocastReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { changeAirPlaneImage(); } } /** * * 项目名称:ToolWidget * 类名称: GPSListener * 类描述:GPS状态监听 * 创建人:陈伟斌 * 创建时间:2012-2-10 下午03:04:19 * */ class GPSListener implements LocationListener{ @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { iv_gps.setImageResource(R.drawable.gps_off); } @Override public void onProviderEnabled(String provider) { iv_gps.setImageResource(R.drawable.gps_on); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/switchbar_background" android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical|center_horizontal" android:orientation="horizontal" > <ImageView android:id="@+id/bluetooth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/bluetooth_on" /> <ImageView android:id="@+id/brightness" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/brightness_most" /> <ImageView android:id="@+id/sound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/sound_on" /> <ImageView android:id="@+id/wifi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/wifi_enabled" /> <ImageView android:id="@+id/flymodel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/fly_model_off" /> <ImageView android:id="@+id/gps" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_weight="1" android:src="@drawable/gps_on" /> </LinearLayout>源码下载: http://download.csdn.net/detail/c_weibin/4142010