Saturday, February 2, 2013

Android invoke application to connect to internet/wifi ig Connection is not present

We can start the intents if the connection is not available. after invoking certain intents we can connect and use the services which are desired for our application.

Make a project with the name AndroidDetectInternetConnection and have class AndroidDetectInternetConnectionActivity.java as main activity with the given following code


package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity extends Activity {

// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btnStatus = (Button) findViewById(R.id.btn_check);

// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());

/**
* Check Internet status button click event
* */
btnStatus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// get Internet status
isInternetPresent = cd.isConnectingToInternet();

// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(
AndroidDetectInternetConnectionActivity.this,
"Internet Connection",
"You have internet connection", true);
} else {
// Internet connection is not present
// Ask user to connect to Internet
// showAlertDialog(AndroidDetectInternetConnectionActivity.this,
// "No Internet Connection",
// "You don't have internet connection.", false);
Intent intent=new Intent(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);

ComponentName cName = new ComponentName("com.android.phone","com.android.phone.Settings");

intent.setComponent(cName);
startActivity(intent);
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
}

});

}

/**
* Function to display simple Alert Dialog

* @param context
*            - application context
* @param title
*            - alert dialog title
* @param message
*            - alert message
* @param status
*            - success/failure (used to set icon)
* */
public void showAlertDialog(Context context, String title, String message,
Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();

// Setting Dialog Title
alertDialog.setTitle(title);

// Setting Dialog Message
alertDialog.setMessage(message);

// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});

// Showing Alert Message
alertDialog.show();
}
}

the main.xml is given below


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Detect Internet Status" />
    
    <Button android:id="@+id/btn_check"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Check Internet Status"
        android:layout_centerInParent="true"/>

</RelativeLayout>

the mainfest is showing below for the permissions and other settings

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.detectinternetconnection"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidDetectInternetConnectionActivity"
            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>
    
    <!-- Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

You can download the code from the link AndroidConnectionUtility



No comments:

Post a Comment