【发布时间】:2013-12-08 07:51:48
【问题描述】:
我需要编写一个应用程序来打开设备,这是一个 htc 平板电脑,以控制设备的 wifi 开和关设置以及 gps 开和关设置。应用程序可以控制这些吗?如果可以,我以前从未使用过系统设置吗?任何教育 pdf、网站或 youtube 视频也会很棒。非常感谢。
【问题讨论】:
我需要编写一个应用程序来打开设备,这是一个 htc 平板电脑,以控制设备的 wifi 开和关设置以及 gps 开和关设置。应用程序可以控制这些吗?如果可以,我以前从未使用过系统设置吗?任何教育 pdf、网站或 youtube 视频也会很棒。非常感谢。
【问题讨论】:
使用以下权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
您可以使用
进行检查boolean wifiEnabled = wifiManager.isWifiEnabled()
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
为GPS
使用这个
private void turnGPSOn(){
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.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
【讨论】: