标签:des android style blog http color os io 使用
在Android源码中,提供的快捷开关相对是比较少的,Android4.0系统默认提供的桌面快捷开关AppWidget上只有5种开关(分别是Wifi开关、蓝牙开关、GPS开关、同步开关、亮度设置开关)如下图所示:
当然,有时候就需要开发实现承载更多的快捷开关的AppWidget来实现用户体验,所以,本文主要针对这些开关的主要代码实现来重点解决开发这些快捷开关。
本文涉及到的快捷开关代码实现有Wifi、蓝牙、GPS、同步、亮度设置、飞行模式、移动数据流量(实现开启和关闭移动网络)、静音模式、重启、关机、锁屏、屏幕旋转等。需要注意的是:实现这些开关控制时都需要在AndroidManifest.xml文件中添加相应的权限。
一般这些开关在被设置改变时,系统会向外界发送相应的广播。所以,当用代码实现操作这些开关时,我们可以通过动态注册广播接收器,来接收这些系统发送的状态改变广播,以此来验证我们是否正常设置改变了这些开关。
当然,在本文以下的一些实例代码中,开关按钮也随着状态的改变而显示不同的文字,动态注册广播接收会显得有点多余,不过这只是证明系统会发送相应的广播,还应用开发还是有用处的,至少我们可以在不同的进程中监听接收这些广播。
1. Wifi开关:
1). Wifi开关由WifiManager这个类控制实现。
2). 当Wifi开关改变时,系统会向外界发送广播android.net.wifi.WIFI_STATE_CHANGED;
示列代码如下:
- package com.example.wst;
-
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class WifiSwitchTest extends Activity implements OnClickListener
- {
- private WifiManager mWifiManager;
- private Button mWifiButton;
-
- public static final String WIFI_STATE_CHANGED = "android.net.wifi.WIFI_STATE_CHANGED";
- private TestChange mTestChange;
- private IntentFilter mIntentFilter;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
- mTestChange = new TestChange();
- mIntentFilter = new IntentFilter();
-
- mIntentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
-
- mWifiButton = (Button)findViewById(R.id.wifi);
- refreshButton();
- mWifiButton.setOnClickListener(this);
- }
-
- @Override
- protected void onDestroy()
- {
-
- super.onDestroy();
-
- unregisterReceiver(mTestChange);
- }
-
- @Override
- protected void onResume()
- {
-
- super.onResume();
-
- registerReceiver(mTestChange, mIntentFilter);
- }
-
-
- private void refreshButton()
- {
- mWifiButton.setText(mWifiManager.isWifiEnabled() ? R.string.wifi_off : R.string.wifi_on);
- }
-
- @Override
- public void onClick(View v)
- {
-
- if (mWifiManager.isWifiEnabled())
- {
-
- mWifiManager.setWifiEnabled(false);
- }
- else
- {
-
- mWifiManager.setWifiEnabled(true);
- }
- }
-
- private class TestChange extends BroadcastReceiver
- {
-
- @Override
- public void onReceive(Context context, Intent intent)
- {
-
- String action = intent.getAction();
-
- if (WIFI_STATE_CHANGED.equals(action))
- {
- refreshButton();
- Toast.makeText(WifiSwitchTest.this, "Wifi设置有改变",
- Toast.LENGTH_SHORT).show();
- }
- }
-
- }
- }
权限添加:
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
2. 蓝牙开关:
1). 蓝牙开关主要调用BluetoothAdapter相关方法实现
2). 蓝牙有四种状态:正在打开、打开、正在关闭、关闭
3). 蓝牙状态改变,系统向外界发送广播android.bluetooth.adapter.action.STATE_CHANGED或android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED;
示例代码如下:
权限添加:
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
3. 屏幕旋转开关:
1). 屏幕旋转开关设置主要调用android.provider.Settings.System的putInt和getInt方法实现。
2). 通过ContentObserver来动态观察屏幕旋转设置的改变。
示例代码如下:
- package com.example.srs;
-
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.database.ContentObserver;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.provider.Settings;
- import android.provider.Settings.SettingNotFoundException;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class ScreenRotationSwitch extends Activity implements OnClickListener
- {
- private Button mRotationButton;
-
- private RotationObserver mRotationObserver;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mRotationObserver = new RotationObserver(new Handler());
-
- mRotationButton = (Button) findViewById(R.id.rotation);
- refreshButton();
- mRotationButton.setOnClickListener(this);
- }
-
-
- @Override
- protected void onDestroy() {
-
- super.onDestroy();
-
- mRotationObserver.stopObserver();
- }
-
-
- @Override
- protected void onResume() {
-
- super.onResume();
-
- mRotationObserver.startObserver();
- }
-
-
-
- private void refreshButton()
- {
- if (getRotationStatus(this) == 1)
- {
- mRotationButton.setText(R.string.rotation_off);
- }
- else
- {
- mRotationButton.setText(R.string.rotation_on);
- }
- }
-
-
- private int getRotationStatus(Context context)
- {
- int status = 0;
- try
- {
- status = android.provider.Settings.System.getInt(context.getContentResolver(),
- android.provider.Settings.System.ACCELEROMETER_ROTATION);
- }
- catch (SettingNotFoundException e)
- {
-
- e.printStackTrace();
- }
- return status;
- }
-
- private void setRotationStatus(ContentResolver resolver, int status)
- {
-
- Uri uri = android.provider.Settings.System.getUriFor("accelerometer_rotation");
-
- android.provider.Settings.System.putInt(resolver, "accelerometer_rotation", status);
-
- resolver.notifyChange(uri, null);
- }
-
- @Override
- public void onClick(View v)
- {
-
-
- if (getRotationStatus(this) == 1)
- {
-
- setRotationStatus(getContentResolver(), 0);
- }
- else
- {
- setRotationStatus(getContentResolver(), 1);
- }
- }
-
-
- private class RotationObserver extends ContentObserver
- {
- ContentResolver mResolver;
-
- public RotationObserver(Handler handler)
- {
- super(handler);
- mResolver = getContentResolver();
-
- }
-
-
- @Override
- public void onChange(boolean selfChange)
- {
-
- super.onChange(selfChange);
-
- refreshButton();
- Toast.makeText(ScreenRotationSwitch.this, "旋转屏幕设置有变化",
- Toast.LENGTH_SHORT).show();
- }
-
- public void startObserver()
- {
- mResolver.registerContentObserver(Settings.System
- .getUriFor(Settings.System.ACCELEROMETER_ROTATION), false,
- this);
- }
-
- public void stopObserver()
- {
- mResolver.unregisterContentObserver(this);
- }
- }
- }
权限添加:
- <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
4. 同步开关:
1). 同步开关设置主要由ContentResolver类(抽象类)的静态函数来实现;
2). 当同步模式改变时,系统会向外界发送广播com.android.sync.SYNC_CONN_STATUS_CHANGED;
示例代码如下:
权限添加:
- <uses-permission android:name="android.permission.READ_SYNC_STATS" />
- <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
- <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
5. 亮度设置开关:
1). 亮度设置的主要调用Settings.System的putInt和getInt方法来处理,已经调用PowerManager的setBacklightBrightness方法来实现调节手机亮度。
2). PowerManager的setBacklightBrightness的方法是隐藏的,通过反射来调用实现。
3). 通过ContentObserver来动态观察亮度设置的改变。
示例代码如下:
- package com.example.bs;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.database.ContentObserver;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.PowerManager;
- import android.provider.Settings;
- import android.provider.Settings.SettingNotFoundException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class BrightnessSwitch extends Activity implements OnClickListener
- {
- private Button mBrightness;
- private static final int LIGHT_NORMAL = 64;
- private static final int LIGHT_50_PERCENT = 127;
- private static final int LIGHT_75_PERCENT = 191;
- private static final int LIGHT_100_PERCENT = 255;
- private static final int LIGHT_AUTO = 0;
- private static final int LIGHT_ERR = -1;
-
- private BrightObserver mBrightObserver;
- private PowerManager mPowerManager;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
-
- mBrightObserver = new BrightObserver(new Handler());
-
- mBrightness = (Button)findViewById(R.id.bright);
- refreshButton();
- mBrightness.setOnClickListener(this);
- }
-
-
- @Override
- protected void onDestroy()
- {
-
- super.onDestroy();
- mBrightObserver.stopObserver();
- }
-
-
- @Override
- protected void onResume()
- {
-
- super.onResume();
- mBrightObserver.startObserver();
- }
-
-
- private void refreshButton()
- {
- switch (getBrightStatus())
- {
- case LIGHT_NORMAL:
- mBrightness.setText(R.string.light_50percent);
- break;
- case LIGHT_50_PERCENT:
- mBrightness.setText(R.string.light_75percent);
- break;
- case LIGHT_75_PERCENT:
- mBrightness.setText(R.string.light_100percent);
- break;
- case LIGHT_100_PERCENT:
- mBrightness.setText(R.string.light_auto);
- break;
- case LIGHT_AUTO:
- mBrightness.setText(R.string.light_normal);
- break;
- case LIGHT_ERR:
- mBrightness.setText(R.string.light_err);
- break;
- }
- }
-
-
- private int getBrightStatus()
- {
-
-
- int light = 0;
- boolean auto = false;
- ContentResolver cr = getContentResolver();
-
- try
- {
- auto = Settings.System.getInt(cr,
- Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
- if (!auto)
- {
- light = android.provider.Settings.System.getInt(cr,
- Settings.System.SCREEN_BRIGHTNESS, -1);
- if (light > 0 && light <= LIGHT_NORMAL)
- {
- return LIGHT_NORMAL;
- }
- else if (light > LIGHT_NORMAL && light <= LIGHT_50_PERCENT)
- {
- return LIGHT_50_PERCENT;
- }
- else if (light > LIGHT_50_PERCENT && light <= LIGHT_75_PERCENT)
- {
- return LIGHT_75_PERCENT;
- }
- else if (light > LIGHT_75_PERCENT && light <= LIGHT_100_PERCENT)
- {
- return LIGHT_100_PERCENT;
- }
- }
- else
- {
- return LIGHT_AUTO;
- }
- }
- catch (SettingNotFoundException e1)
- {
-
- e1.printStackTrace();
- }
- return LIGHT_ERR;
-
- }
-
- private void setBrightStatus()
- {
- int light = 0;
-
- switch (getBrightStatus())
- {
- case LIGHT_NORMAL:
- light = LIGHT_50_PERCENT - 1;
- break;
- case LIGHT_50_PERCENT:
- light = LIGHT_75_PERCENT - 1;
- break;
- case LIGHT_75_PERCENT:
- light = LIGHT_100_PERCENT - 1;
- break;
- case LIGHT_100_PERCENT:
- startAutoBrightness(getContentResolver());
- break;
- case LIGHT_AUTO:
- light = LIGHT_NORMAL - 1;
- stopAutoBrightness(getContentResolver());
- break;
- case LIGHT_ERR:
- light = LIGHT_NORMAL - 1;
- break;
-
- }
-
- setLight(light);
- setScreenLightValue(getContentResolver(), light);
- }
-
-
- private void setLight(int light)
- {
- try
- {
-
- Class<?> pmClass = Class.forName(mPowerManager.getClass().getName());
-
- Field field = pmClass.getDeclaredField("mService");
- field.setAccessible(true);
-
- Object iPM = field.get(mPowerManager);
-
- Class<?> iPMClass = Class.forName(iPM.getClass().getName());
-
- Method method = iPMClass.getDeclaredMethod("setBacklightBrightness", int.class);
- method.setAccessible(true);
-
- method.invoke(iPM, light);
- }
- catch (ClassNotFoundException e)
- {
-
- e.printStackTrace();
- }
- catch (NoSuchFieldException e)
- {
-
- e.printStackTrace();
- }
- catch (IllegalArgumentException e)
- {
-
- e.printStackTrace();
- }
- catch (IllegalAccessException e)
- {
-
- e.printStackTrace();
- }
- catch (NoSuchMethodException e)
- {
-
- e.printStackTrace();
- }
- catch (InvocationTargetException e)
- {
-
- e.printStackTrace();
- }
-
- }
-
- @Override
- public void onClick(View v)
- {
-
- setBrightStatus();
- }
-
-
- public void startAutoBrightness(ContentResolver cr)
- {
- Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS_MODE,
- Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
- }
-
-
- public void stopAutoBrightness(ContentResolver cr)
- {
- Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS_MODE,
- Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
- }
-
-
- public void setScreenLightValue(ContentResolver resolver, int value)
- {
- android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS,
- value);
- }
-
- private class BrightObserver extends ContentObserver
- {
- ContentResolver mResolver;
-
- public BrightObserver(Handler handler)
- {
- super(handler);
- mResolver = getContentResolver();
- }
-
- @Override
- public void onChange(boolean selfChange)
- {
-
- super.onChange(selfChange);
- refreshButton();
- Toast.makeText(BrightnessSwitch.this, "亮度设置有改变", Toast.LENGTH_SHORT).show();
- }
-
-
- public void startObserver()
- {
- mResolver.registerContentObserver(Settings.System
- .getUriFor(Settings.System.SCREEN_BRIGHTNESS), false,
- this);
- mResolver.registerContentObserver(Settings.System
- .getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE), false,
- this);
- }
-
-
- public void stopObserver()
- {
- mResolver.unregisterContentObserver(this);
- }
- }
- }
权限添加:
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <uses-permission android:name="android.permission.DEVICE_POWER" />
6. 飞行模式开关:
1). 飞行模式主要是调用Settings.System的getInt和setInt方法来处理。
2). 当飞行模式改变时,系统会向外界发送广播android.intent.action.AIRPLANE_MODE;
示例代码如下:
- package com.example.apmst;
-
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class AirplaneModeSwitchTest extends Activity implements OnClickListener
- {
- private Button mAirplane;
-
- private static final String AIRPLANE_MODE = "android.intent.action.AIRPLANE_MODE";
- private TestChange mTestChange;
- private IntentFilter mIntentFilter;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mTestChange = new TestChange();
- mIntentFilter = new IntentFilter();
-
- mIntentFilter.addAction("android.intent.action.AIRPLANE_MODE");
-
- mAirplane = (Button)findViewById(R.id.airplane);
- refreshButton();
-
- mAirplane.setOnClickListener(this);
- }
-
- @Override
- protected void onDestroy()
- {
-
- super.onDestroy();
-
- unregisterReceiver(mTestChange);
- }
-
- @Override
- protected void onResume()
- {
-
- super.onResume();
-
- registerReceiver(mTestChange, mIntentFilter);
- }
-
-
- private void refreshButton()
- {
- mAirplane.setText(getAirplaneModeStatus() ? R.string.airplane_off : R.string.airplane_on);
- }
-
-
-
- private boolean getAirplaneModeStatus()
- {
- boolean status = Settings.System.getInt(this.getContentResolver(),
- Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true : false;
-
- return status;
- }
-
-
- private void setAirplaneMode(Context context, boolean enable)
- {
- Settings.System.putInt(context.getContentResolver(),
- Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0);
- Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
- intent.putExtra("state", enable);
- context.sendBroadcast(intent);
- }
-
- @Override
- public void onClick(View v)
- {
-
- if (getAirplaneModeStatus())
- {
- setAirplaneMode(this, false);
- }
- else
- {
- setAirplaneMode(this, true);
- }
- }
-
- private class TestChange extends BroadcastReceiver
- {
-
- @Override
- public void onReceive(Context context, Intent intent)
- {
-
- String action = intent.getAction();
-
- if (AIRPLANE_MODE.equals(action))
- {
- refreshButton();
- Toast.makeText(AirplaneModeSwitchTest.this, "飞行模式设置有改变",
- Toast.LENGTH_SHORT).show();
- }
- }
-
- }
- }
权限添加:
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
7. 移动数据流量开关:
1). 移动数据流量由ConnectivityManager类控制实现,这个类实现设置和获取移动流量状态的方法是隐藏的,所以我们只能通过反射来实现(或者在源码下编译APK)。
2). 相关广播为android.intent.action.ANY_DATA_STATE;
示例代码如下:
权限添加:
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
- <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
8. 静音模式开关:
1). 静音模式由AudioManager控制实现,有三种状态:正常(有声音)、震动、静音
2). 当模式改变时,系统会向外界发送广播android.media.RINGER_MODE_CHANGED;
示例代码如下:
静音模式开关设置不需要添加权限。
<-----以下的开关设置实现需要有系统的UID使用Platform的apk签名,否则是没有权限调用的,会报SecurityException异常----->
注:1). 可以不通过apk签名,直接在android源码下编译生成apk,再将apk安装到手机;
2). 如果手机有root权限,那么也不需要apk签名,直接通过adb工具将apk推到system/app目录下:操作指令为adb remount---->adb push xxx.apk system/app (注:该apk将做为系统应用)
9. GPS开关:
1). GPS开关设置的实现由Secure类的相关静态方法实现。
2).Secure的isLocationProviderEnabled和setLocationProviderEnabled调用需要APK签名;
示例代码如下:
- package com.example.gst;
-
- import android.app.Activity;
- import android.content.Context;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.provider.Settings.Secure;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class GpsSwitchTest extends Activity implements OnClickListener
- {
- private Button mGpsButton;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mGpsButton = (Button)findViewById(R.id.gps);
- refreshButton();
- mGpsButton.setOnClickListener(this);
- }
-
-
- private void refreshButton()
- {
- mGpsButton.setText(getGpsStatus(this) ? R.string.gps_off : R.string.gps_on);
- }
-
-
- private boolean getGpsStatus(Context context)
- {
- boolean status = Secure.isLocationProviderEnabled(context.getContentResolver(),
- LocationManager.GPS_PROVIDER);
- return status;
- }
-
-
- private void setGpsStatus(Context context, boolean enabled)
- {
- Secure.setLocationProviderEnabled(context.getContentResolver(),
- LocationManager.GPS_PROVIDER, enabled);
- }
-
- @Override
- public void onClick(View v)
- {
-
- if (getGpsStatus(this))
- {
- setGpsStatus(this, false);
- mGpsButton.setText(R.string.gps_on);
- }
- else
- {
- setGpsStatus(this, true);
- mGpsButton.setText(R.string.gps_off);
- }
- }
- }
权限添加:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
10. 锁屏:
1). 手机进入锁屏主要由PowerManager的goToSleep函数实现。
2). PowerManager的goToSleep调用需要apk签名。
示例代码:
- package com.example.lsst;
-
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.PowerManager;
- import android.os.SystemClock;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class LockScreenSwitchTest extends Activity implements OnClickListener
- {
- private PowerManager mPowerManager;
- private Button mLockButton;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
- mLockButton = (Button)findViewById(R.id.lock);
- mLockButton.setOnClickListener(this);
- }
-
- private void lockScreen()
- {
-
- mPowerManager.goToSleep(SystemClock.uptimeMillis());
- }
- @Override
- public void onClick(View v)
- {
-
- lockScreen();
- }
- }
权限添加:
- <uses-permission android:name="android.permission.USES_POLICY_FORCE_LOCK" />
- <uses-permission android:name="android.permission.DEVICE_POWER" />
11. 重启:
1). 手机重启需要调用PowerManager的reboot方法实现,参数为null;
2). 该方法的调用,需要有系统的UID使用Platform的APK签名,否则是没有权限调用的,会报SecurityException异常。
示例代码如下:
- package com.example.rs;
-
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.PowerManager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class RebootSwitch extends Activity implements OnClickListener
- {
- private Button mRebootButton;
- private PowerManager mPowerManager;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
- mRebootButton = (Button)findViewById(R.id.reboot);
- mRebootButton.setOnClickListener(this);
- }
-
- private void reboot(String reason)
- {
- mPowerManager.reboot(null);
- }
- @Override
- public void onClick(View v)
- {
-
- reboot(null);
- }
- }
权限添加:
- <uses-permission android:name="android.permission.REBOOT"/>
12. 关机:
1). 手机关机直接通过创建相关的Intent来启动一个对话框,根据对话框的确认或取消键来选择是否关机
2). 关机实现需要apk签名。
示例代码如下:
- package com.example.sds;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class ShutDownSwitch extends Activity implements OnClickListener
- {
- private Button mShutDown;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mShutDown = (Button)findViewById(R.id.shutdown);
- mShutDown.setOnClickListener(this);
-
- }
- @Override
- public void onClick(View v)
- {
-
- Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
- intent.putExtra("android.intent.extra.KEY_CONFIRM", true);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- startActivity(intent);
- }
-
- }
权限添加:
- <uses-permission android:name="android.permission.SHUTDOWN" />
ok,本文的快捷开关代码实现介绍就到此结束,有了上面这些快捷开关的实现代码,那么当你想要开发一个AppWidget来承载和实现这些开关时就容易多了,至于如何去开发一个AppWidget,有兴趣的读者可以去找找相关这些方面的资料。
相关代码下载链接:http://download.csdn.net/detail/stevenhu_223/5572751
转载自:http://blog.csdn.net/stevenhu_223/article/details/9052083
Android快捷开关实现(转)
标签:des android style blog http color os io 使用
原文地址:http://www.cnblogs.com/SharkBin/p/3949942.html