Fixed API lvl < 17 quotation mark issue and wifi deactivation on CompoundButton isChecked==true

This commit is contained in:
Gerome Bochmann 2015-02-24 21:02:37 +01:00
parent 221659c745
commit f7097d0cb5
3 changed files with 25 additions and 6 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4"> <module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="tueroeffner" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager"> <component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle"> <facet type="android-gradle" name="Android-Gradle">
<configuration> <configuration>
@ -9,6 +9,7 @@
<facet type="android" name="Android"> <facet type="android" name="Android">
<configuration> <configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" /> <option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" /> <option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" /> <option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" /> <option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
@ -24,6 +25,7 @@
</component> </component>
<component name="NewModuleRootManager" inherit-compiler-output="false"> <component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" /> <output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" /> <sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />

View file

@ -7,6 +7,7 @@ import android.content.Intent;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Build;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
@ -34,6 +35,12 @@ public class DerivBroadcastReceiver extends BroadcastReceiver {
if (nwInfo != null && nwInfo.isConnectedOrConnecting()) { if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
String ssid = wifiInfo.getSSID(); String ssid = wifiInfo.getSSID();
// Build.VERSION.SDK_INT is incompatible with device API lvl < 4 use String.toInteger(Build.VERSION.SDK) or maybe find some other way ^_^
if (Build.VERSION.SDK_INT > 17){
targetSSID.replace("\"", "");
}
if (ssid.equals(targetSSID)){ if (ssid.equals(targetSSID)){
buttonActivator((Activity) context, true); buttonActivator((Activity) context, true);
Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show();

View file

@ -48,7 +48,7 @@ import java.util.List;
public class MainActivity extends ActionBarActivity { public class MainActivity extends ActionBarActivity {
final static String networkSSID = "KrautSpace"; static String networkSSID = "KrautSpace";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -60,6 +60,7 @@ public class MainActivity extends ActionBarActivity {
.commit(); .commit();
} }
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ; intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
@ -132,7 +133,16 @@ public class MainActivity extends ActionBarActivity {
Context context = rootView.getContext(); Context context = rootView.getContext();
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
if (isChecked) { if (isChecked) {
WifiConfiguration conf = new WifiConfiguration();
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
/*WifiConfiguration conf = new WifiConfiguration();
conf.SSID = networkSSID; conf.SSID = networkSSID;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int retval = wifiManager.addNetwork(conf); int retval = wifiManager.addNetwork(conf);
@ -147,10 +157,10 @@ public class MainActivity extends ActionBarActivity {
} }
} }
wifiManager.disconnect(); wifiManager.disconnect();
wifiManager.reconnect(); wifiManager.reconnect();*/
Toast.makeText(buttonView.getContext(), getString(R.string.wlan_activated), Toast.LENGTH_SHORT).show(); Toast.makeText(buttonView.getContext(), getString(R.string.wlan_activated), Toast.LENGTH_SHORT).show();
} else { } else {
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); /*List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) { for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals(networkSSID)) { if(i.SSID != null && i.SSID.equals(networkSSID)) {
wifiManager.removeNetwork(i.networkId); wifiManager.removeNetwork(i.networkId);
@ -159,7 +169,7 @@ public class MainActivity extends ActionBarActivity {
} }
} }
wifiManager.disconnect(); wifiManager.disconnect();
wifiManager.reconnect(); wifiManager.reconnect();*/
Toast.makeText(buttonView.getContext(), getString(R.string.wlan_deactivated), Toast.LENGTH_SHORT).show(); Toast.makeText(buttonView.getContext(), getString(R.string.wlan_deactivated), Toast.LENGTH_SHORT).show();
} }
} }