I'm developing an Android Application in which I have to verify whether the Device in Online or Offline mode (ie., I Have to verify whether the Connectivity Service is available or not ).
I've two classes class A and Class B .
In class A , I tried the code Snippets like below,
package com.example.onlineverification;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//new OnlineCheck().onCreate(savedInstanceState)
Toast.makeText(MainActivity.this,"Online Verification "+new OnlineCheck().isOnline(), Toast.LENGTH_LONG).show();
}
});
}
}
In Class B,the Code Snippets that I've tried is given below,
package com.example.onlineverification;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
public class OnlineCheck extends Activity
{
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conMgr.getAllNetworkInfo();
boolean state=false;
for(NetworkInfo nInfo:networkInfo){
if(nInfo.getType()==ConnectivityManager.TYPE_WIFI || nInfo.getType()==ConnectivityManager.TYPE_ETHERNET || nInfo.getType()==ConnectivityManager.TYPE_MOBILE){
if (nInfo.getState() == NetworkInfo.State.CONNECTED
|| nInfo.getState() == NetworkInfo.State.CONNECTING) {
state=true;
break;
}
}
}
return state;
}
}
when I click the button , the Application crashed and my Logcat error is given below,
08-23 10:55:29.152: E/AndroidRuntime(25945): FATAL EXCEPTION: main
08-23 10:55:29.152: E/AndroidRuntime(25945): java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.app.Activity.getSystemService(Activity.java:4463)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.example.onlineverification.OnlineCheck.isOnline(OnlineCheck.java:18)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.example.onlineverification.MainActivity$1.onClick(MainActivity.java:24)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.view.View.performClick(View.java:4204)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.view.View$PerformClick.run(View.java:17355)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Handler.handleCallback(Handler.java:725)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Looper.loop(Looper.java:137)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-23 10:55:29.152: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 10:55:29.152: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-23 10:55:29.152: E/AndroidRuntime(25945): at dalvik.system.NativeStart.main(Native Method)