1

I have developed an android 2.1 application which consumes Soap Web Service. When my application gets started, it first checks whether there is internet connectivity or not.

If so, it will display corresponding activity. If not then it will display an Activity(NetworkErrorActivity) giving information about network errors and all.

The problem is if there is no internet connectivity, it shows me the NetworkErrorActivity. Now when user presses back button it redirects the user to Home. I have overriden onBackPressed method like this :

@Override public void onBackPressed() {

        Intent setIntent = new Intent(Intent.ACTION_MAIN);
        setIntent.addCategory(Intent.CATEGORY_HOME);
        setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(setIntent); 
        return;
    }

After pressing back button, it shows me the android home screen. But the problem is when i start the same application again, it shows me the NetworkErrorActivity even if there is network connectivity. Could not start the application from Main Launcher Activity. It always shows me the same Activity again and again.

8
  • show the code where you check for network and start the activity
    – ingsaurabh
    Mar 14, 2011 at 6:58
  • See this : try{ androidHttpTransport = new HttpTransportSE(URL,30000); androidHttpTransport.call(SOAP_ACTION,envelope); so = (SoapObject)envelope.bodyIn; String st=so.toString(); //Do some processing..... } catch( Exception e ) { Log.i("no internet", e.getMessage() ); this.cancel(true); Intent intent=new Intent(context,NetworkErrorActivity.class); startActivity(intent); } Mar 14, 2011 at 7:04
  • is every time your log printed if yes then there is some exception that's not related to network but making catch code to run every time so debug your code for any exception
    – ingsaurabh
    Mar 14, 2011 at 7:08
  • Yaa. Actually I was finding the problem so used Log for that. Mar 14, 2011 at 7:15
  • so whats its displaying every time ?
    – ingsaurabh
    Mar 14, 2011 at 7:20

2 Answers 2

1

I use an AlertDialog (not an Activity) to inform the user about needing a network connection with an option to take them to network settings (to enable mobile/wi-fi) connections. If they choose to click 'Cancel' instead I force the main activity to finish().

Check for network connection in main activity onResume() and if there's no connection then call CreateNetErrorDialog()...it isn't possible to use BACK to dismiss the dialog - only Cancel which kills the main activity.

Example...

protected void CreateNetErrorDialog(String errorMessage) {
    Log.d(TAG, "CreateNetErrorDialog() entered...");

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(errorMessage)
    .setTitle("Unable to connect")
    .setCancelable(false)
    .setPositiveButton("Settings",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(i);
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MyMainActivity.this.finish();
            }
        }
    );
    AlertDialog alert = builder.create();
    alert.show();
    Log.d(TAG, "Exiting CreateNetErrorDialog()...");
}

EDIT: Call the CreateNetErrorDialog from the onPostExecute() method of your AsyncTask - it runs on the UI thread (doInBackground doesn't). Example from my own code...

private class FileDownloader extends AsyncTask<String, Void, Void> {
    private Boolean Success = false;
    private String ResultString = "";

    @Override
    protected Void doInBackground(String... params) {
        try {
            // Do whatever
        }
        catch (Exception e) { // <-- Use the correct exception type
              ResultString = "Some error message";
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        if (!Success)
            CreateNetErrorDialog (ResultString);
        else
            // Do whatever
    }
}
6
  • What i want to do is when there is no internet connection, i want to display something telling user that you dont have an internet connection or network error...and then want to force the application to shutdown Can you suggest me what will be the best for doing this ? Mar 14, 2011 at 7:29
  • 1
    @Kartik: What I was trying to show was instead of starting an Activity to tell the user there is a network problem, try an AlertDialog like the one I show in my code (change the message to whatever you need it to say). The Settings button will give them the option to go to Android Settings to enable wi-fi/mobile internet if they need to - when they return from settings, if the connect fails again, show the dialog again. If they use Cancel on the AlertDialog it will call finish() on you main activity and it isn't possible to use BACK to quit the AlertDialog so would fix your problem.
    – Squonk
    Mar 14, 2011 at 7:41
  • @ MisterSquonk : Yaa you are absolutely correct. See what I am doing is when my application is started, it will check for internet connection. I have used AsynTask for background processing. I am using network timeout parameter ? (I dont know is it good or not). If network timeout happens, it throws InterruptedException. In catch block i am calling the alertdialog. but its not showing me the dialog. Mar 14, 2011 at 7:47
  • @Kartik: Is your AsyncTask nested within your main activity or is it in a separate class file?
    – Squonk
    Mar 14, 2011 at 7:56
  • 1
    @Kartik: See my edit to the end of my original answer and also I changed CreateNetErrorDialog to take a String parameter for the dialog message. You need to call it from the onPostExecute() method of AsyncTask.
    – Squonk
    Mar 14, 2011 at 8:13
0

I don't know exactly what you want but try something like this instead of making a connection and writing code in catch block which is not a good practice

to check for network use following code

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();

and then depending on result of above code

if (ni == null)
{
      //Show you network error activity
}
else
{
    //Show some other activity
}
2
  • I tried for this also. But it always showed me connected. :( I think the problem is : I am connected to LAN but not logged in for internet usage in firewall. I need to change my IP I think or use an individual PC ?? Mar 14, 2011 at 7:50
  • Yaa correct. Let me go for this. Will check it and post comment again about this. Mar 14, 2011 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.