Month: June 2012

  • How to turn on / turn off WiFi connection programmatically in Android

    How to turn on / turn off WiFi connection programmatically in Android

    Some Android applications can turn on/turn off WiFi on device. How to do same.

    First of all you need specify uses-permission in Android Manifest file

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

    And now you can enable wifi state via WiFiManager:

    wifiManager.setWifiEnabled(true);

    or disable:

    wifiManager.setWifiEnabled(false);

    Below you can download source code of sample android project

    The source code of main Activity is:

    package com.hrupin.wifiSample;
    
    import android.app.Activity;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ToggleButton;
    import com.hrupin.wifiSample.R;
    
    public class WifiSwitcherExampleActivity extends Activity implements OnClickListener {
        private static ConnectivityManager connectivityManager;
        private ToggleButton tbWiFi;
        private WifiManager wifi;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            tbWiFi = (ToggleButton) findViewById(R.id.toggleButtonWiFi);
            tbWiFi.setOnClickListener(this);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            initButton();
        }
    
        private void initButton() {
            if (isConnected(this, ConnectivityManager.TYPE_WIFI)) {
                tbWiFi.setChecked(true);
            }
        }
    
        private static boolean isConnected(Context context, int networkType) {
            NetworkInfo networkInfo = null;
            if (connectivityManager != null) {
                networkInfo = connectivityManager.getNetworkInfo(networkType);
            }
            return networkInfo == null ? false : networkInfo.isConnected();
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == tbWiFi.getId()) {
                wifi.setWifiEnabled(tbWiFi.isChecked());
            }
        }
    }

    Android Manifest:

    <!--?xml version="1.0" encoding="utf-8"?-->
    <manifest 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.hrupin.wifiSample" 
        android:versioncode="1" 
        android:versionname="1.0">
    
        <uses-sdk android:minsdkversion="8">
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
        <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission></uses-permission></uses-sdk>
    
        <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
            <activity android:name=".WifiSwitcherExampleActivity" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>

    Here you can download the source code of sample Android application.

    Download it from github
  • How to deal with Lucida Grande Regular & Lucida Grande Bold font in ActionScript3

    How to deal with Lucida Grande Regular & Lucida Grande Bold font in ActionScript3

    To embed Lucida Grande Regular &/or Lucida Grande Bold font in ActionScript3 you need to do next:

    ....
    ....
    ....
    // declaring fonts
    [Embed(source="fonts/Lucida_Grande_Regular.ttf", fontFamily="Lucida_Grande_Regular", fontWeight="normal", mimeType="application/x-font-truetype", embedAsCFF="false")]
    private var LucidaGrandeRegular:Class;
    [Embed(source="fonts/Lucida_Grande_Bold.ttf", fontFamily="Lucida_Grande_Bold", fontWeight="normal", mimeType="application/x-font-truetype", embedAsCFF="false")]
    private var LucidaGrandeBold:Class;
    ....
    ....
    ....
    ....
    // For Regular Font
    // Create TextFormat with embeded font
    var format:TextFormat = new TextFormat();
    format.size = 17;
    format.font = "Lucida_Grande_Regular";
    format.bold = false;
    format.italic = false;
    format.color = 0xFFFFFF;
    format.align = TabAlignment.CENTER;
    // Use TextFormat
    var text:TextField = new TextField();
    text.embedFonts = true;
    text.defaultTextFormat = format;
    
    ....
    ....
    ....
    
    // For Bold Font
    // Create TextFormat with embeded font
    var format2:TextFormat = new TextFormat();
    format2.size = 17;
    format2.font = "Lucida_Grande_Bold";
    format2.bold = false;
    format2.italic = false;
    format2.color = 0xFFFFFF;
    format2.align = TabAlignment.CENTER;
    // Use TextFormat
    var text2:TextField = new TextField();
    text2.embedFonts = true;
    text2.defaultTextFormat = format2;

    That’s it :).
    Please, write questions in comments.
    Don’t forget share this post 🙂