1

Sorry to ask this question after searching in http://stackoverflow.com. But I couldn't process to next activity when using these below codes at different section.

Here when user Not connected internet then a popup will alert and says "you not connected to internet" in alertdialog and when user click on exit button in alertdilog the app will close. if user have internet connection a popup will alert and says "you have internet connection" in alertdialog and when user click on continue button app continues to next activity. I am having problem in continuing to next activity, How to do that?

Below codes are show in their section

Code in AndroidDetectInternetConnectionActivity.java

package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
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
        AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
        builder.setMessage("You have network connection.")
            .setTitle("Internet Connection")
            .setCancelable(false);
    AlertDialog alert = builder.create();
                    alert.setButton("Continue", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which){ 
                          // Do call some activity. Do what you    wish to;
startActivity(new Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
                           }
                          }); 
                    alert.show();
            }

 else {
                // Internet connection is not present
                // Ask user to connect to Internet
      AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
       builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
         .setTitle("No Internet Connection")
         .setCancelable(false);
    AlertDialog alert = builder.create();
    alert.setButton2("Exit", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                         //close the program
                    AndroidDetectInternetConnectionActivity.this.finish();
        }
            });
                      // Showing Alert Message
               alert.show();
                 }
        }

Code in ConnectionDetector.java package com.example.detectinternetconnection;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}
}

Code in AndroidManifest.xml

  <?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>
    <activity android:name=".MainActivity2"></activity>
  </application>

 <!-- Internet Permissions -->
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 </manifest>

Tell me where I am doing wrong?

Boann
  • 48,794
  • 16
  • 117
  • 146
user3260468
  • 21
  • 2
  • 8

3 Answers3

0

Add this to the beginning of your Activity:

import android.content.Intent;
FD_
  • 12,947
  • 4
  • 35
  • 62
  • Thanks worked.. but getting error in AlertDialog alert = builder.create(); alert.setButton2("Exit", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) says "The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)" – user3260468 Feb 04 '14 at 08:11
0

Use:

Intent mainIntent;

                    mainIntent = new Intent(CurrentClass.this,ClassToBeLoaded.class); 

                    CurrentClass.this.startActivity(mainIntent);
                    CurrentClass.this.finish();

Also press Ctrl+Shift+ O once done

The above process adds all the required imports to your class file.

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • Thanks worked. but another error here i am getting AlertDialog alert = builder.create(); alert.setButton2("Exit", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { //close the program AndroidDetectInternetConnectionActivity.this.finish(); } }); – user3260468 Feb 04 '14 at 08:08
  • error says "The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)" – user3260468 Feb 04 '14 at 08:08
  • Refer to Ranjit Patils answer for the above problem! – Skynet Feb 04 '14 at 08:09
  • Also if the above helped you, you can accept my answer by clicking on the tick mark next to it:) – Skynet Feb 04 '14 at 08:11
0

Total code is fine, but just create initialize the alertdialog at last through create()..

    if (isInternetPresent) {              
    // Internet Connection is Present
   // make HTTP requests
    AlertDialog.Builder builder = new   AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
    builder.setMessage("You have network connection.")
        .setTitle("Internet Connection")
        .setCancelable(false);

                alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which){ 
                      // Do call some activity. Do what you    wish to;
         startActivity(new  Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
                  }
               }); 
          AlertDialog alert = builder.create();
                alert.show();

EDIT

Here is your total Activity Class.

package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity  extends Activity implements   OnClickListener {

Boolean isInternetPresent = false;
ConnectionDetector cd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Button btnStatus = (Button) findViewById(R.id.btnStatus);
    cd = new ConnectionDetector(getApplicationContext());
    btnStatus.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {

        AlertDialog alert;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                AndroidDetectInternetConnectionActivity .this);
        builder.setTitle("Internet Connection");
        builder.setCancelable(false);

        builder.setPositiveButton("Continue",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(AndroidDetectInternetConnectionActivity .this,
                                MainActivity2 .class));
                    }
                });

        alert = builder.create();
        alert.show();
    }

    else {

        AlertDialog alert;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                AndroidDetectInternetConnectionActivity .this);
        builder.setTitle("No Internet Connection");
        builder.setMessage("Please Connect To Internet");
        builder.setCancelable(false);

        builder.setPositiveButton("Exit",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        alert = builder.create();
        alert.show();
    }
}

}

I think you will take care of other classes.For some knowledge you may refer the Android Dialog tutorials on developer site.

Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • else { AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this); builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.") .setTitle("No Internet Connection") .setCancelable(false); AlertDialog alert = builder.create(); alert.setButton2("Exit", new **DialogInterface**.**OnClickListener**(){ public void onClick(DialogInterface dialog, int which) { AndroidDetectInternetConnectionActivity.this.finish(); } }); // Showing Alert Message **alert**.show(); } – user3260468 Feb 04 '14 at 08:26
  • in first blod error is The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int) – user3260468 Feb 04 '14 at 08:28
  • click ctrl + shift + O ..as Nun Chai said...for auto implementation. this errors are comming coz you do'nt import the appropreate packages. – Ranjit Feb 04 '14 at 09:20
  • Sir send me your email id. Nanu kuda karnataka davanu. – user3260468 Feb 04 '14 at 09:33
  • error are got cleared but during running the app I got these error [2014-02-04 16:32:13 - AndroidDetectInternetConnection] HOME is up on device 'emulator-5556' [2014-02-04 16:32:13 - AndroidDetectInternetConnection] Uploading AndroidDetectInternetConnection.apk onto device 'emulator-5556' [2014-02-04 16:32:14 - AndroidDetectInternetConnection] Installing AndroidDetectInternetConnection.apk... – user3260468 Feb 04 '14 at 11:07
  • [2014-02-04 16:34:22 - AndroidDetectInternetConnection] Failed to install AndroidDetectInternetConnection.apk on device 'emulator-5556! [2014-02-04 16:34:22 - AndroidDetectInternetConnection] (null) [2014-02-04 16:34:22 - AndroidDetectInternetConnection] Launch canceled! – user3260468 Feb 04 '14 at 11:08
  • Now I am getting like this [2014-02-04 16:47:15 - AndroidDetectInternetConnection] Automatic Target Mode: using existing emulator 'emulator-5556' running compatible AVD 'andr' [2014-02-04 16:47:15 - AndroidDetectInternetConnection] Uploading AndroidDetectInternetConnection.apk onto device 'emulator-5556' [2014-02-04 16:47:16 - AndroidDetectInternetConnection] Installing AndroidDetectInternetConnection.apk... – user3260468 Feb 04 '14 at 11:19
  • [2014-02-04 16:47:24 - AndroidDetectInternetConnection] Success! [2014-02-04 16:47:25 - AndroidDetectInternetConnection] Starting activity com.example.detectinternetconnection.AndroidDetectInternetConnectionActivity on device emulator-5556 [2014-02-04 16:47:27 - AndroidDetectInternetConnection] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.detectinternetconnection/.AndroidDetectInternetConnectionActivity } in emulator it say **Unfortunately AndroidDetectInternetConnection has stoped** – user3260468 Feb 04 '14 at 11:21
  • please Check Code in **AndroidManifest.xml** above, all permission is allowed but application is not opening after install – user3260468 Feb 04 '14 at 11:24
  • These are your run time error. you have to check your logcat and ask a new question for this with logcat errors not console – Ranjit Feb 04 '14 at 11:34