从非静态上下文中调用静态方法

时间:2015-11-08 12:39:53

标签: java android

我试图从我的广播接收器中的onReceive方法调用SCS类中的一些方法,但它在任何地方都给出了错误"无法对非静态方法进行静态引用CheckConnection()来自SCS"

类型

SCSReceiver.class

public class SCSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    SCS.mehtodName(context);
    // ... do what you need to do here...
}

SCS.class

public class SCS extends Activity {
private Handler handler;
private Runnable runnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_stablizer);
}
public static void mehtodName(Context context) {
    // TODO Auto-generated method stub
    CheckConnection();
}

private void CheckConnection() {
    ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cn.getActiveNetworkInfo();
    if (nf != null && nf.isConnected() == true) {
        //CheckAccess();
        Toast.makeText(this,
                "Network Available! now checking Inernet access..",
                Toast.LENGTH_SHORT).show();
    } else {
        // Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT)
        // .show();
    }
}

的manifest.xml

<activity
        android:name="com.xyz.netspeedmaster.SCS"
        android:label="@string/title_activity_connection_stablizer" >
    </activity>

     <receiver android:name="com.xyz.abc.SCSReceiver" >
    </receiver>

2 个答案:

答案 0 :(得分:1)

使SCSReceiver类成为SCS类的内部类:

public class SCS extends Activity {
private Handler handler;
private Runnable runnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_stablizer);
}
public static void methodName(Context context) {
    // TODO Auto-generated method stub
    CheckConnection();
}

private void CheckConnection() {
    ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cn.getActiveNetworkInfo();
    if (nf != null && nf.isConnected() == true) {
        CheckAccess();
        Toast.makeText(this,
                "Network Available! now checking Inernet access..",
                Toast.LENGTH_SHORT).show();
    } else {
        // Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT)
        // .show();
    }
}

class SCSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    methodName(context);
    // ... do what you need to do here...
}

}

不需要SCS.methodName,只需直接调用该方法即可。

答案 1 :(得分:1)

如果您不想基于系统接收器调用BroadcastReceiver,那么您可以尝试使用Dynamic BroadcastReceiver,通过它可以调用Activity的方法。

public class SCS extends Activity {
private Handler handler;
private Runnable runnable;
private IntentFilter filter;
private BroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_stablizer);

    filter=new IntentFilter("com.your_package.ANY_TEXT_STRING");
    receiver=new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
                 mehtodName(context);  //Invoke your methods from here
                    }
           };
       registerReceiver(filter,receiver);

       //Place the below two lines where you want to trigger BroadcastReceiver
       Intent intent=new Intent("com.your_package.ANY_TEXT_STRING");
       sendBroadcast(intent);

}
public static void mehtodName(Context context) {
    // TODO Auto-generated method stub
    CheckConnection();
}

private void CheckConnection() {
    ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cn.getActiveNetworkInfo();
    if (nf != null && nf.isConnected() == true) {
        //CheckAccess();
        Toast.makeText(this,
                "Network Available! now checking Inernet access..",
                Toast.LENGTH_SHORT).show();
    } else {
        // Toast.makeText(this, "Network Not Available", Toast.LENGTH_SHORT)
        // .show();
    }
}