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

代码出处: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,String,Class,button)