在不使用特定活动的情况下从android发送意图

在不使用特定活动的情况下从android发送意图,android,class,import,Android,Class,Import,我不确定这是否可能只是我正在做的事情。我有一个应用程序首选项类,已导入到我的活动中。从这个类中,我运行了一个check-internetconnection函数,如果返回null,我将向另一个活动发送一个意图。问题是我想在整个应用程序中运行此函数。是否有方法获取当前活动的名称并将其传递给AppsReferences类,然后将其放置在intent中 这是我到目前为止的功能 public boolean isOnline() { ConnectivityManager cm =

我不确定这是否可能只是我正在做的事情。我有一个应用程序首选项类,已导入到我的活动中。从这个类中,我运行了一个check-internetconnection函数,如果返回null,我将向另一个活动发送一个意图。问题是我想在整个应用程序中运行此函数。是否有方法获取当前活动的名称并将其传递给AppsReferences类,然后将其放置在intent中

这是我到目前为止的功能

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");

}

尝试将当前活动的上下文作为参数发送到方法

主叫班

Context myContext = this;
obj.checkInternet(myContext);
公共类,所有类都可以调用它来检查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");

谢谢你的建议,我想我理解你所说的逻辑,但我不确定如何实现它。嗨,我已经编辑了我的代码,说明了我如何尝试实现它,但仍然在eclipse中出错