Piyush
Piyush

Reputation: 2609

Internet Connection in Android

I have work on Application That use Internet.

my issue is that, When device is connected to internet than my Apps works fine But when Device is not connected to internet "Progress dialog" is running for infinite time.

i want to stop dialog if device is not connected to internet or wifi. how to do this?
Any idea? Sorry guys.... I have change my mind i want to check internet connection when i click on button. what i have tried so far is..

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        mclip = (ImageButton) findViewById(R.id.clip);
        mclip.setClickable(true);
        mclip.setFocusable(true);

        mclip.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                ConnectivityManager cm = (ConnectivityManager)              getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isConnectedOrConnecting()) {
               //if it is connected to internet than start Another Activity.
                startActivity(new Intent(ListViewExample.this, ClipOfDay.class));
                } else if (netInfo == null) {
                    AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
                    alertDialog.setTitle("Connection Problem");
                    alertDialog.setMessage("You are not connected to Internet");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }

            }
        });
       }

But this is not working.if device is not connected to internet than i want to show AlertDialog. otherwise it will start Activity. can Anybody tell me what to do?
Thanks.

Upvotes: 0

Views: 2527

Answers (3)

Aman Aalam
Aman Aalam

Reputation: 11251

Use these two methods to check connectivity:

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_WIFI)

then parse the results returned to know whether the connection is available. If its available, then only do the things and put the dialog box.

alternatively, you could also pop the dialog box up, and before doing any network action, check for connectivity with these methods, and if connection isn't available, hide/cancel the dialog box and display an error message. else, go for the network action. and yes, always have a try-catch block.

Upvotes: 1

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34833

Check network connection
A simple code for checking connection

ConnectivityManager cMgr = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
            String status = netInfo.getState().toString();
            if (status.equals("CONNECTED")) {
               //DO you work
            } else {
                Log.e("error", "No connection available ");
            }

Upvotes: 2

viv
viv

Reputation: 6177

  • First check if internet or wifi is connected, then run your code
  • Use try, catch, in case some exception occurs because of connection error, remove dialog in catch block.



This can be one way of doing this..............
Hope it helps.......

Upvotes: 0

Related Questions