Richard Low
Richard Low

Reputation: 57

calling a boolean method from another class

I am trying to call a boolean from another class, to check for internet connection.

calling code is

public void submit_user(View view){
    EditText editText = (EditText) findViewById(R.id.new_username);
    String user_name = editText.getText().toString();
    EditText pw = (EditText)findViewById(R.id.new_password);
    String pwd = pw.getText().toString();

    MyActivity my = new MyActivity();
    if(my.isConnected()) {
        //do something
    }
}

boolean method is:

public boolean isConnected(){
    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }else{
        return false;
    }
}

error message:

FATAL EXCEPTION: main

                                                                                   Caused by: java.lang.reflect.InvocationTargetException
                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:525)


                                                                                   Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

tried to post error message, but won't let me submit.

new at this, appreciate help. Thank you in advance.

Richard

Upvotes: 1

Views: 3948

Answers (4)

bambi
bambi

Reputation: 33

declare method public static and access using that class name in another class

for example method in ClassOne.java and you want to use the from from classone in ClasssTwo.java

simply use ClassOne.isConnceted()

Upvotes: 0

Haroon
Haroon

Reputation: 505

public static boolean isConnected(Context context){
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
    return true;
}else{
    return false;
 }
}

Put this method inside simple java class not Activity

Upvotes: 0

Sathish Kumar J
Sathish Kumar J

Reputation: 4345

You can't use new with Activity class. so try with following ways.

use static method like,

public static boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
    return true;
}else{
    return false;
}

}

and then access directly like,

if(MyActivity.isConnected()) {
    //do something
}

or

create object like,

MyActivity myActivity = ((MyActivity)getApplicationContext());

then access,

 if(myActivity.isConnected()) {
    //do something
}

Thank you.

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

IllegalStateException: System services not available to Activities before onCreate()

Means MyActivity class extending Activity and currently not running.

Instead of calling methods from class which is extending Activity or using other application main components like Services,BroadcastRecivers,... create a normal java class and pass Context of caller class to access System Service from other class. change isConnected method as :

public boolean isConnected(Context mContext){
    ConnectivityManager connMgr = (ConnectivityManager)mContext.
            getSystemService(Context.CONNECTIVITY_SERVICE);
  ....
}

and pass view.getContext() or Activity Context to isConnected method :

MyActivity my = new MyActivity();
if(my.isConnected(view.getContext())) {
    //do something
}

also remove extends Activity from MyActivity class

Upvotes: 0

Related Questions