简体   繁体   中英

Sending intents without using a specific activity to go from android

I'm not sure if this is possible just something I'm working on. I have a app preferences class that's imported into my activity. From that class I run a check internet connection function that if returned null I send an intent to another activity. Problem is I want to run this function throughout my app is there a way to get the name of the current activity and pass it to the appspreferences class and place it within the intent.

Here is my function so far

public boolean isOnline() {
        ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }   


private ConnectivityManager getSystemService(String connectivityService) {
    // TODO Auto-generated method stub
    return null;
}


public void checkInternet(){

    isOnline();

    if(isOnline() == false){

        this.getClass().getSimpleName();

Intent connectionIntent = new Intent(this.getClass().getSimpleName().this,Network.class);
        this.getClass().getSimpleName().this.startActivity(connectionIntent);
        this.getClass().getSimpleName().this.finish();  

    }
    Log.v("Pref", "checking internet");

}

try sending context of current Activity as parameter to your method

calling class

Context myContext = this;
obj.checkInternet(myContext);

common Class which can be called by all classes to check for internet

    public void checkInternet(Context myContext){

    isOnline();

    if(isOnline() == false){

Intent connectionIntent = new Intent(myContext,Network.class);
        startActivity(connectionIntent);
        myContext.finish();  

    }
    Log.v("Pref", "checking internet");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM