【问题标题】:Sending intents without using a specific activity to go from android在不使用特定活动的情况下从 android 发送意图
【发布时间】:2012-09-06 07:55:12
【问题描述】:

我不确定这是否可能只是我正在做的事情。我有一个导入到我的活动中的应用程序偏好类。从该类中,我运行一个检查互联网连接功能,如果返回 null,我将意图发送到另一个活动。问题是我想在我的应用程序中运行这个函数有没有办法获取当前活动的名称并将其传递给 apppreferences 类并将其放置在意图中。

这是我目前的功能

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

}

【问题讨论】:

    标签: android class import


    【解决方案1】:

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

    调用类

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

    所有类都可以调用以检查互联网的公共类

        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 中仍然出错
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多