-2

Check connection is Available

is there a way to find out if the device is connected to the Internet and then start activity else toast a message when click on the button.

JcDenton86
  • 933
  • 1
  • 12
  • 32
Mir Khan
  • 3
  • 3
  • 2
    Did you [search](http://stackoverflow.com/questions/9570237/android-check-internet-connection) before you ask ;)? – JcDenton86 Aug 12 '15 at 13:20
  • yes i searched and applied many codes but app cursh when i run on emulator – Mir Khan Aug 12 '15 at 13:20
  • 2
    provide proper internet permissions.. – Arun Shankar Aug 12 '15 at 13:21
  • 1
    Then it would be a good idea to share some code – JcDenton86 Aug 12 '15 at 13:21
  • i want if connection is available then start activity else toast the message ` protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); enterForJoke=(Button)findViewById(R.id.welcomeBtn); final Intent intent=new Intent(this,Jokecatagory.class); enterForJoke.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(intent); } });//end of the On Click Listner } ` – Mir Khan Aug 12 '15 at 13:24
  • i am sorry i don't know how to paste code in comment box hope you understand sorry to say that. – Mir Khan Aug 12 '15 at 13:26
  • 1
    Edit the question and add your code over there. And as far as I can see, there's no code of yours, which checks for the connection. Also, as your application is crashing, post the logcat too in your question. – Prerak Sola Aug 12 '15 at 13:27

2 Answers2

1

Have a look on Android's Developer pages, here is a sort example:

You need to have these two permissions in your AndroidManifest.xml file

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

Then you can do something like the following to check network connectivity:

 ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
    // start your activity here
} else {
    // display your toast here
}

For a more extended example you can see here:

http://developer.android.com/training/basics/network-ops/connecting.html

jrsall92
  • 572
  • 1
  • 5
  • 19
  • 1
    While the URL, may answer the question, it is not a good practice to post them as answers. The URL might change in future and it might not be accessible to some users in certain scenarios. Instead, post the relevant code from the URL, which might solve the issue as per you. And you can give the URL as a reference. – Prerak Sola Aug 12 '15 at 13:24
  • 1
    Edited it @PrerakSola , my bad. – jrsall92 Aug 12 '15 at 13:30
0
if (isNetworkAvailable())
{
       Network is here..write your code here
}
else 
{
    // Notify user they aren't connected
    Toast.makeText(getApplicationContext(),"You aren't connected to the internet.", Toast.LENGTH_SHORT).show();

}  


private boolean isNetworkAvailable() 
{
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

use this method,if netwok available than it return "true", otherwise "false" of boolean type..

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
Bhavin Kevadiya
  • 224
  • 3
  • 16