android open

package com.example.opengps_wifi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void open3g(View view) {
		toggleMobileData(getApplicationContext(), true);
		System.out.println("open3g");
	}

	public void openwifi(View view) {
		toggleWiFi(getApplicationContext(), true);
		System.out.println("openwifi");

	}

	public void close3g(View view) {
		toggleMobileData(getApplicationContext(), false);
		System.out.println("close3g");
	}

	public void closewifi(View view) {
		toggleWiFi(getApplicationContext(), false);
		System.out.println("closewifi");
	}

	public void changegps(View view) {
		// Settings.Secure.setLocationProviderEnabled(getContentResolver(),
		// "gps", true);

		try {
			Class secureClass = Class
					.forName("android.provider.Settings$Secure");
			Method isMethod = secureClass.getMethod(
					"isLocationProviderEnabled", ContentResolver.class,
					String.class);
			Boolean ret = (Boolean) isMethod.invoke(secureClass,
					this.getContentResolver(), "gps");
			System.out.println(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}
		toggleGPS();
		
		turnGPSOn();
		togglegps2();
		System.out.println("changegps");
	}

	// 设置是否打开Wifi ok
	private void toggleWiFi(Context context, boolean enabled) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		wifiManager.setWifiEnabled(enabled);
	}

	// 设置是否打开Wifi ok
	private void toggleMobileData2(Context context, boolean enabled) {
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			Class class1 = Class.forName("android.net.ConnectivityManager");
			Method method1 = class1.getMethod("setMobileDataEnabled",
					new Class[] { boolean.class });
			method1.setAccessible(true);
			method1.invoke(connectivityManager, enabled);
			System.out.println("toggleMobileData2");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * GPS开关
	 * <p>
	 * 当前若关则打开
	 * <p>
	 * 当前若开则关闭
	 */
	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(this, 0, gpsIntent, 0).send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

	public void togglegps2() {
		LocationManager alm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
			Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
		}
		Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
		startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面

	}
	
	public void turnGPSOn()
	{
	     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
	     intent.putExtra("enabled", true);
	    // getApplicationContext().sendBroadcast(intent);

	    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
	    if(!provider.contains("gps")){ //if gps is disabled
	        final Intent poke = new Intent();
	        poke.setComponent(new ComponentName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"));
	        
	        
	        //poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
	        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
	        poke.setData(Uri.parse("3")); 
	        getApplicationContext().sendBroadcast(poke);
	    }
	}

	private boolean isGPSEnable() {
		/*
		 * 用Setting.System来读取也可以,只是这是更旧的用法 String str =
		 * Settings.System.getString(getContentResolver(),
		 * Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		 */
		String str = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		Log.v("GPS", str);
		if (str != null) {
			return str.contains("gps");
		} else {
			return false;
		}
	}

	/**
	 * 移动网络开关
	 */
	private void toggleMobileData(Context context, boolean enabled) {
		ConnectivityManager conMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		Class<?> conMgrClass = null; // ConnectivityManager类
		Field iConMgrField = null; // ConnectivityManager类中的字段
		Object iConMgr = null; // IConnectivityManager类的引用
		Class<?> iConMgrClass = null; // IConnectivityManager类
		Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
		try {
			// 取得ConnectivityManager类
			conMgrClass = Class.forName(conMgr.getClass().getName());
			// 取得ConnectivityManager类中的对象mService
			iConMgrField = conMgrClass.getDeclaredField("mService");
			// 设置mService可访问
			iConMgrField.setAccessible(true);
			// 取得mService的实例化类IConnectivityManager
			iConMgr = iConMgrField.get(conMgr);
			// 取得IConnectivityManager类
			iConMgrClass = Class.forName(iConMgr.getClass().getName());
			// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
			setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod(
					"setMobileDataEnabled", Boolean.TYPE);
			// 设置setMobileDataEnabled方法可访问
			setMobileDataEnabledMethod.setAccessible(true);
			// 调用setMobileDataEnabled方法
			setMobileDataEnabledMethod.invoke(iConMgr, enabled);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值