【问题标题】:Broadcast receiver: check the wifi presence广播接收器:检查 wifi 存在
【发布时间】:2013-05-03 20:01:39
【问题描述】:

我已经编写了一个利用广播接收器的代码,以便在连接状态更改时调用 Receive 上的方法并显示一个指示更改的 toast。从这个意义上说,我有一个单独的类,其中以这种方式实现广播接收器:

public class Receiver extends BroadcastReceiver {

private static final String DEBUG_TAG = "NetworkStatusExample";

  @Override

  public void onReceive(Context context, Intent intent) {

      // Shows a toast when the connectivity state change
      Toast.makeText(context, "stato wifi cambiato",
      Toast.LENGTH_LONG).show();
      Log.d("prova", "action: "
                 + intent.getAction())     

  }
}

当网络状态发生变化并显示 toast 时,它可以正确调用 OnReceive() 方法。

在主文件中,我放置了一个代码,一旦按下按钮,它就会验证 Wifi 是否已连接,在这种情况下,也会根据存在或未显示消息“Wifi 已连接:真/假”来显示祝酒词不是连通性。在这种情况下也可以使用的代码是:

public class MainActivity extends Activity {

private static final String DEBUG_TAG = "NetworkStatusExample";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo =     connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    final boolean isWifiConn = networkInfo.isConnected();
    String wifiStateText = "No State";
    Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
    Button prova = (Button) findViewById(R.id.button1);
    prova.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = getApplicationContext();

            CharSequence text = "Wifi connected: " + isWifiConn;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    });

}

现在我正在尝试将代码的最后一部分嵌入到上面定义的方法 OnReceive() 中,以便在网络连接发生变化时自动执行所有操作,因此我得到的广播接收器代码是那个:

public class Receiver extends BroadcastReceiver {

private static final String DEBUG_TAG = "NetworkStatusExample";
  // Verifica se il wifi è connesso

  @Override

  public void onReceive(Context context, Intent intent) {

      // Shows a toast when the connectivity state change
      Toast.makeText(context, "stato wifi cambiato",
      Toast.LENGTH_LONG).show();
      Log.d("prova", "action: "
                 + intent.getAction());


      ConnectivityManager connMgr = (ConnectivityManager) 
                getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
            final boolean isWifiConn = networkInfo.isConnected();
          String wifiStateText = "No State";
            Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);

      // Toast that shows if the wifi is connected
      CharSequence text = "Wifi connected: " + isWifiConn;
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();     


  }


}

在这种情况下,我会收到此错误:

the method getsystemservice(string) is undefined for the type Receiver

在这一行:

getSystemService(Context.CONNECTIVITY_SERVICE);

怎么了?我该怎么做才能修复它?

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    需要使用'context'参数:context.getSystemService(Context.CONNECTIVITY_SERVICE);

    BroadcastReceiver 不是从Context 派生的。

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      context.getSystemService(Context.CONNECTIVITY_SERVICE);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多