1. Download our Official Android App: Forums for Android!

Apps Newbie Help

Discussion in 'Android Development' started by dobes, May 2, 2012.

  1. dobes

    dobes Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    May 2, 2012

    May 2, 2012
    1
    0
    5
    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;
    }
    }
     

    Advertisement

  2. MSUgEEk

    MSUgEEk Android Expert
    Rank:
    None
    Points:
    313
    Posts:
    3,696
    Joined:
    Apr 22, 2010

    Apr 22, 2010
    3,696
    1,418
    313
    Electrical Design Engineer
    Northwestern Alabama
    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.
     
  3. GeorgeN

    GeorgeN Well-Known Member
    Rank:
    None
    Points:
    73
    Posts:
    132
    Joined:
    Apr 16, 2012

    Apr 16, 2012
    132
    38
    73
    Male
    London
    Just construct an object with new:

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

Share This Page

Loading...