网络本机模块android在android studio程序上工作时不能在android的react-native程序中工作 [英] network native module android not working in react-native progam for android while working on android studio program

查看:109
本文介绍了网络本机模块android在android studio程序上工作时不能在android的react-native程序中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 android 服务没有太多经验,broadcastReceiver 也有一些警告 WifiReceiver.java 使用或覆盖已弃用的 API.我搜索了这个但找不到任何解决方案,我不知道哪种方法已被弃用,我对 java 和 android 的接触不多

I have not too much experience with android services, the broadcastReceiver have some warning also WifiReceiver.java uses or overrides a deprecated API. I search for this but couldn't find any solution, I don't know which one method is deprecated, I m not in touch too much with java and 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 重新编译以获取详细信息.

Note: Recompile with -Xlint:deprecation for details.

推荐答案

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

use with react application content reactContext.getApplicationContext().registerReceiver(args here)

这篇关于网络本机模块android在android studio程序上工作时不能在android的react-native程序中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆