Reputation: 1178
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)
Upvotes: 0
Views: 8407
Reputation: 79
On 2.Activity Import MainActivity first
For example You are on 2.Activity and You will call method from 2.Activity On MainActivity name of method callMethode()
Activity 1 (For Example on MainActivity)
public static void callMethode(Context context){
// Your codes Here
}
Activity 2
MainActivity.callMethode(getApplicationContext());
Upvotes: 0
Reputation: 4382
This is your B class
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
public class OnlineCheck
{
public boolean isOnline(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context
.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;
}
}
And call with following code
Toast.makeText(MainActivity.this,"Online Verification "+ new OnlineCheck().isOnline(MainActivity.this), Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 11440
The problem is you are creating an instance of your OnlineCheck without going through the proper channels for creating an Activity. You should either change activities to your online activity or move the isOnline() into MainActivity.
See How to start new activity on button click for starting an activity from inside another one.
The crux of it is you cannot just call new MyActivity(). Activities are not designed to be initialized that way.
EDIT:
This could work to if you really really want it in the OnlineCheck class
Toast.makeText(MainActivity.this,"Online Verification "+OnlineCheck.isOnline(this), Toast.LENGTH_LONG).show();
public class OnlineCheck extends Activity
{
public static boolean isOnline(Activity source) {
ConnectivityManager conMgr = (ConnectivityManager) source
.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;
}
}
Upvotes: 0
Reputation: 83008
Do not call that method from Activity. Create a separate class and define that method as static. like below
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtils {
public static boolean isOnline(Context mContext) {
ConnectivityManager conMgr = (ConnectivityManager) mContext
.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;
}
}
And call this method in Activity as
NetworkUtils.isOnline(MainActivity.this);
In your case
Toast.makeText(MainActivity.this,"Online Verification "+NetworkUtils.isOnline(MainActivity.this), Toast.LENGTH_LONG).show();
Upvotes: 3
Reputation: 6738
Here you are getting IllegalException it means it is not valid in android
Runtime(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)
I suggest you to do something like this
1) BaseActivity extends Activity
2) OnlineCheck extends BaseActivity
3) MainActivity extends BaseActivity
then code which is used in both activities write it down in BaseActivity.
Upvotes: 0