网络原生模块android在使用Android Studio程序时无法在react-native progam中工作

问题描述 投票:0回答:1

我对android服务没有太多经验,broadcastReceiver也有一些警告WifiReceiver.java使用或覆盖不推荐使用的API。我搜索了此内容,但找不到任何解决方案,我不知道不赞成使用哪种方法,我与java和android的联系不多

这里是错误

cannot find symbol
        registerReceiver(wifiReceiver,filter);
        ^
  symbol:   variable registerReceiver

我的反应方法是

@ReactMethod
    public void NetInfo(){
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        WifiReceiver wifiReceiver = new WifiReceiver();
        registerReceiver(wifiReceiver,filter);
    }

广播接收者是

    public class WifiReceiver extends BroadcastReceiver {
        String TAG = getClass().getSimpleName();
        private Context mContext;

        @Override
        public void onReceive(Context context, Intent intent) {

            mContext = context;


            if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = cm.getActiveNetworkInfo();

                if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
                        networkInfo.isConnected()) {
                    // Wifi is connected
                    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    String ssid = wifiInfo.getSSID();

                    Log.e(TAG, " -- Wifi connected --- " + " SSID " + ssid );
                    Toast.makeText(context, " -- Wifi connected --- " + " SSID " + ssid , Toast.LENGTH_SHORT).show();

                }
            }
            else if (intent.getAction().equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION))
            {
                int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
                if (wifiState == WifiManager.WIFI_STATE_DISABLED)
                {
                    Toast.makeText(context, "Status changed", Toast.LENGTH_SHORT).show();
                }

            }
        }
    }

也获得此注释,

注意:有关详细信息,请使用-Xlint:deprecation重新编译。

android react-native service react-native-android
1个回答
0
投票

与反应应用程序内容reactContext.getApplicationContext().registerReceiver(args here)一起使用

© www.soinside.com 2019 - 2024. All rights reserved.