[Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
来源:互联网 发布:单片机gsm模块 编辑:程序博客网 时间:2024/05/16 13:43
代码出处:http://www.learningandroid.net/blog/advance/programmable-toggle-gps/
自己实现了一下,确实可行。
不足之处是无法监控设置过程中的状态,设置后的最后结果。
- package lab.sodino.togglegps;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.app.PendingIntent.CanceledException;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class ActToggleGPS extends Activity implements OnClickListener {
- private static final int BLUETOOTH = 4;
- private static final int BRIGHTNESS = 1;
- private static final int GPS = 3;
- private static final int SYNC = 2;
- private static final int WIFI = 0;
- private TextView txtInfo;
- private Button btnGPS, btnWifi, btnSync, btnBrightness, btnBluetooth;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btnGPS = (Button) findViewById(R.id.toggleGPS);
- btnGPS.setOnClickListener(this);
- btnBluetooth = (Button) findViewById(R.id.toggleBluetooth);
- btnBluetooth.setOnClickListener(this);
- btnBrightness = (Button) findViewById(R.id.toggleBrightness);
- btnBrightness.setOnClickListener(this);
- btnSync = (Button) findViewById(R.id.toggleSync);
- btnSync.setOnClickListener(this);
- btnWifi = (Button) findViewById(R.id.toggleWifi);
- btnWifi.setOnClickListener(this);
- txtInfo = (TextView) findViewById(R.id.info);
- }
- public static void toggle(Context context, int idx) {
- Intent intent = new Intent();
- intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
- intent.addCategory("android.intent.category.ALTERNATIVE");
- Uri uri = Uri.parse("custom:" + idx);
- intent.setData(uri);
- try {
- PendingIntent.getBroadcast(context, 0, intent, 0).send();
- } catch (CanceledException e) {
- e.printStackTrace();
- }
- }
- public boolean checkGPS(String key) {
- try {
- Class secureClass = Class.forName("android.provider.Settings$Secure");
- Method isMethod = secureClass.getMethod("isLocationProviderEnabled", ContentResolver.class, String.class);
- Boolean result = (Boolean) isMethod.invoke(secureClass, this.getContentResolver(), "gps");
- Log.d("ANDROID_LAB", "gps=" + result);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return false;
- }
- @Override
- public void onClick(View v) {
- if (v == btnGPS) {
- toggle(this, GPS);
- } else if (v == btnWifi) {
- toggle(this, WIFI);
- } else if (v == btnSync) {
- toggle(this, SYNC);
- } else if (v == btnBluetooth) {
- toggle(this, BLUETOOTH);
- } else if (v == btnBrightness) {
- toggle(this, BRIGHTNESS);
- }
- }
- }
原文地址:点击打开链接
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- Android 屏幕亮度调节(2.0以后出现亮度自动调节)如何开启、关闭亮度自动调节
- 【Android】如何调节屏幕亮度,关闭屏幕
- Android调节屏幕亮度、系统音量、wifi、bluetooth、振动等常用设置的整合demo
- Android 屏幕亮度调节
- android调节屏幕亮度
- Android屏幕亮度调节
- android调节屏幕亮度
- Android 调节屏幕亮度
- Android:调节屏幕亮度
- android 调节屏幕亮度
- android调节屏幕亮度
- android调节屏幕亮度
- android屏幕亮度调节
- Android 调节屏幕亮度
- android 调节屏幕亮度
- Android屏幕亮度调节
- MFC原创:三层架构02(人事管理系统)UI-01 .
- Quartz实现动态作业增加
- 矩形 面积并 && 周长并
- 空分区对hive执行的影响
- Ubuntun命令大全
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- poj1004的java实现
- jquery的ajax
- Name That Number USACO
- hdu 4003
- ZooKeeper配置文件参数
- 树状数组模型
- JAVA DAO
- java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法