• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Newbie Help

dobes

Lurker
I have a main activity that is loaded and performs a network check, this works fine.

I would ideally like to move the code for this into another class (most likely in another package to keep things neat), however i do not know how to then call the methods in this class from the original main one.

If i do try and call directly i end up with m methods needing to be made static (which wont work) so assuming that i need to create an instance of the class but can't get the syntax right.

Any help with this would be appreciated as i feel answering this would help me understand the concept.

Current code for main activity is :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NetworkCheck.isInternetOn();
{
Toast.makeText(this, UpdateCheck(), Toast.LENGTH_LONG).show();
}

Current code for NetworkCheck class is :
public class NetworkCheck
{

public final boolean isInternetOn()
{
ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING)
||(connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED))
{
return true;
} else if ((connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED)
|| (connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED))
{
return false;
}
return false;
}
}
 
Can you clarify what you mean by moving it into another package? If you are talking about another apk, I'm not sure that can be done or should be if it can. Help me understand what you are trying to accomplish.
 
Just construct an object with new:

Code:
NetworkCheck checker = new NetworkCheck();
boolean isOn = checker.isInternetOn();
If you put the class in a separate package you need to add an import at the top of your source file.
 
Back
Top Bottom