[Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度

来源:互联网 发布:单片机gsm模块 编辑:程序博客网 时间:2024/05/16 13:43

代码出处:http://www.learningandroid.net/blog/advance/programmable-toggle-gps/


自己实现了一下,确实可行。

不足之处是无法监控设置过程中的状态,设置后的最后结果。


[java] view plaincopy
  1. package lab.sodino.togglegps;  
  2.   
  3. import java.lang.reflect.InvocationTargetException;  
  4. import java.lang.reflect.Method;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.PendingIntent;  
  8. import android.app.PendingIntent.CanceledException;  
  9. import android.content.ContentResolver;  
  10. import android.content.Context;  
  11. import android.content.Intent;  
  12. import android.net.Uri;  
  13. import android.os.Bundle;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.TextView;  
  19.   
  20. public class ActToggleGPS extends Activity implements OnClickListener {  
  21.     private static final int BLUETOOTH = 4;  
  22.     private static final int BRIGHTNESS = 1;  
  23.     private static final int GPS = 3;  
  24.     private static final int SYNC = 2;  
  25.     private static final int WIFI = 0;  
  26.     private TextView txtInfo;  
  27.     private Button btnGPS, btnWifi, btnSync, btnBrightness, btnBluetooth;  
  28.   
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         btnGPS = (Button) findViewById(R.id.toggleGPS);  
  34.         btnGPS.setOnClickListener(this);  
  35.         btnBluetooth = (Button) findViewById(R.id.toggleBluetooth);  
  36.         btnBluetooth.setOnClickListener(this);  
  37.         btnBrightness = (Button) findViewById(R.id.toggleBrightness);  
  38.         btnBrightness.setOnClickListener(this);  
  39.         btnSync = (Button) findViewById(R.id.toggleSync);  
  40.         btnSync.setOnClickListener(this);  
  41.         btnWifi = (Button) findViewById(R.id.toggleWifi);  
  42.         btnWifi.setOnClickListener(this);  
  43.   
  44.         txtInfo = (TextView) findViewById(R.id.info);  
  45.     }  
  46.   
  47.     public static void toggle(Context context, int idx) {  
  48.         Intent intent = new Intent();  
  49.         intent.setClassName("com.android.settings""com.android.settings.widget.SettingsAppWidgetProvider");  
  50.         intent.addCategory("android.intent.category.ALTERNATIVE");  
  51.         Uri uri = Uri.parse("custom:" + idx);  
  52.         intent.setData(uri);  
  53.         try {  
  54.             PendingIntent.getBroadcast(context, 0, intent, 0).send();  
  55.         } catch (CanceledException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.   
  60.     public boolean checkGPS(String key) {  
  61.         try {  
  62.             Class secureClass = Class.forName("android.provider.Settings$Secure");  
  63.             Method isMethod = secureClass.getMethod("isLocationProviderEnabled", ContentResolver.class, String.class);  
  64.             Boolean result = (Boolean) isMethod.invoke(secureClass, this.getContentResolver(), "gps");  
  65.             Log.d("ANDROID_LAB""gps=" + result);  
  66.         } catch (ClassNotFoundException e) {  
  67.             e.printStackTrace();  
  68.         } catch (SecurityException e) {  
  69.             e.printStackTrace();  
  70.         } catch (NoSuchMethodException e) {  
  71.             e.printStackTrace();  
  72.         } catch (IllegalArgumentException e) {  
  73.             e.printStackTrace();  
  74.         } catch (IllegalAccessException e) {  
  75.             e.printStackTrace();  
  76.         } catch (InvocationTargetException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.         return false;  
  80.     }  
  81.   
  82.     @Override  
  83.     public void onClick(View v) {  
  84.         if (v == btnGPS) {  
  85.             toggle(this, GPS);  
  86.         } else if (v == btnWifi) {  
  87.             toggle(this, WIFI);  
  88.         } else if (v == btnSync) {  
  89.             toggle(this, SYNC);  
  90.         } else if (v == btnBluetooth) {  
  91.             toggle(this, BLUETOOTH);  
  92.         } else if (v == btnBrightness) {  
  93.             toggle(this, BRIGHTNESS);  
  94.         }  
  95.     }  
  96. }  

原文地址:点击打开链接

原创粉丝点击