每天写写 2021-10-28 11:39 采纳率: 0%
浏览 18

android 9.0 通过修改源代码解决ip设置的问题

问题已解决

修改packages/apps/Settings
在Setting下面的src的wifi子目录中添加一个广播
WifiBoardcastReceive


```java
package com.android.settings.wifi;

import android.annotation.NonNull;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.content.BroadcastReceiver;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.NetworkRequest;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.net.IpConfiguration;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
import android.view.View;
import android.widget.Toast;
import android.text.TextUtils;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.LinkifyUtils;
import com.android.settings.R;
import android.net.NetworkUtils;
import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.location.ScanningSettings;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener;
import com.android.settings.widget.SwitchBarController;
import com.android.settings.wifi.details.WifiNetworkDetailsFragment;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.wifi.AccessPoint;
import com.android.settingslib.wifi.AccessPoint.AccessPointListener;
import com.android.settingslib.wifi.AccessPointPreference;
import com.android.settingslib.wifi.WifiTracker;
import com.android.settingslib.wifi.WifiTrackerFactory;
import android.net.ProxyInfo;
import com.android.settingslib.core.lifecycle.Lifecycle;
import android.net.StaticIpConfiguration;
import android.net.LinkAddress;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;


public class WifiBoardcastReceive extends BroadcastReceiver  {

    private static final String TAG = "WifiBoardcastReceive";
    private static String mMode = "static";//"static";
    private static String isNew = "new";// new wifi
    private static String SSID = null;//"192.168.1.254";
    private static String mPassword = null;//"192.168.1.254";
    private static String mEthIpAddress = null;//"192.168.1.254";
    private static String mEthGateway = null;//"192.168.1.1";
    private static String mEthNetmask = null;
    private static String networkPrefix = null; // qianzhui
    private static String mEthdns1 = null;//"192.168.1.1";
    private static String mEthdns2 = null;//"null";
    protected WifiManager mWifiManager;



    private void getNetConfigFromIntent(Intent intent){
        Bundle bundle = intent.getExtras();
        if (bundle.getString("isNew") != null)
            this.isNew = bundle.getString("isNew");
        if (bundle.getString("mMode") != null)
            this.mMode = bundle.getString("mMode");
        if (bundle.getString("ipaddr") != null)
            this.mEthIpAddress = bundle.getString("ipaddr");
        if (bundle.getString("netMask")!= null)
            this.mEthNetmask = bundle.getString("netMask");
        if (bundle.getString("gateway")!= null)
            this.mEthGateway = bundle.getString("gateway");
        if (bundle.getString("dns1") != null)
            this.mEthdns1 = bundle.getString("dns1");
        if (bundle.getString("dns2") != null)
            this.mEthdns2 = bundle.getString("dns2");
        if (bundle.getString("SSID") != null)
            this.SSID = bundle.getString("SSID");
        if (bundle.getString("mPassword") != null)
            this.mPassword = bundle.getString("mPassword");
        if (bundle.getString("networkPrefix") != null)
            this.networkPrefix = bundle.getString("networkPrefix");
    }


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

        Log.w(TAG,"onReceive BoardCast");

        android.arch.lifecycle.LifecycleOwner lifecycleOwner = new android.arch.lifecycle.LifecycleOwner() {
            @NonNull
            @Override
            public android.arch.lifecycle.Lifecycle getLifecycle() {
                return null;
            }
        };
        Lifecycle lifecycle = new Lifecycle(lifecycleOwner);

        WifiTracker mWifiTracker = WifiTrackerFactory.create(
                context, null, lifecycle, true, true);
        mWifiManager = mWifiTracker.getManager();
        getNetConfigFromIntent(intent);
        WifiConfiguration config = new WifiConfiguration();
        if(this.SSID==null){
            Log.i(TAG,"onReceive NULL BOARDCAST");
            return;
        }
        config.SSID = AccessPoint.convertToQuotedString(this.SSID);
        config.hiddenSSID = true;
        if(this.mPassword == null){
            this.mPassword = "123456";
        }
        config.shared = true;
        int length = this.mPassword.length();
        if ((length == 10 || length == 26 || length == 58)
                && this.mPassword.matches("[0-9A-Fa-f]*")) {
            //      config.wepKeys[0] = this.mPassword;
        } else {
            //      config.wepKeys[0] = '"' + this.mPassword + '"';
        }

        if (this.mPassword.matches("[0-9A-Fa-f]{64}")) {
            config.preSharedKey = this.mPassword;
        } else {
            config.preSharedKey = '"' + this.mPassword + '"';
        }
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        //config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        //config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        //config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        //config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        //config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        //config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        ///config.status = WifiConfiguration.Status.ENABLED;


        Log.i(TAG,"SSID: "+config.SSID +" mPassword: "+this.mPassword);
        Log.i(TAG,"IpAddress: "+this.mEthIpAddress +" gateway: "+this.mEthGateway);
        Log.i(TAG,"dns1: "+this.mEthdns1 +" dns2: "+this.mEthdns2);


        IpAssignment mIpAssignment = IpAssignment.STATIC;

        ProxySettings mProxySettings = ProxySettings.NONE;
        StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
        ProxyInfo mHttpProxy = null;
        if(this.mMode.equals("DHCP")){
            mIpAssignment = IpAssignment.DHCP;
        }else{
            mIpAssignment = IpAssignment.STATIC;
            Inet4Address inetAddr = getIPv4Address(this.mEthIpAddress);
            if (inetAddr == null || inetAddr.equals(Inet4Address.ANY)) {
                Log.d(TAG,"wifi_ip_settings_invalid_ip_address");
            }
            int networkPrefixLength = -1;
            try {
                networkPrefixLength = Integer.parseInt(this.networkPrefix);
                if (networkPrefixLength < 0 || networkPrefixLength > 32) {
                    Log.d(TAG,"wifi_ip_settings_invalid_network_prefix_length");
                }
                staticIpConfiguration.ipAddress = new LinkAddress(inetAddr, networkPrefixLength);
            } catch (NumberFormatException e) {
                Log.d(TAG,"wifi_network_prefix_length_hint");
            } catch (IllegalArgumentException e) {
                Log.d(TAG,"wifi_ip_settings_invalid_ip_address");
            }
            String gateway = this.mEthGateway;
            if (TextUtils.isEmpty(gateway)) {
                try {
                    InetAddress netPart = NetworkUtils.getNetworkPart(inetAddr, networkPrefixLength);
                    byte[] addr = netPart.getAddress();
                    addr[addr.length - 1] = 1;
                    String hostAddress = InetAddress.getByAddress(addr).getHostAddress();
                    Log.d(TAG,"hostAddress: "+hostAddress);
                } catch (RuntimeException ee) {
                } catch (java.net.UnknownHostException u) {
                }
            } else {
                InetAddress gatewayAddr = getIPv4Address(gateway);
                if (gatewayAddr == null) {
                    Log.d(TAG,"wifi_ip_settings_invalid_gateway");
                }
                if (gatewayAddr.isMulticastAddress()) {
                    Log.d(TAG,"wifi_ip_settings_invalid_gateway");
                }
                staticIpConfiguration.gateway = gatewayAddr;
            }

            String dns = this.mEthdns1;
            InetAddress dnsAddr = null;

            InetAddress dnsAddr = null;
            if (TextUtils.isEmpty(dns)) {
                Log.d(TAG,"wifi_dns1_hint");
            } else {
                dnsAddr = getIPv4Address(dns);
                if (dnsAddr == null) {
                    Log.d(TAG,"wifi_ip_settings_invalid_dns1");
                }
                staticIpConfiguration.dnsServers.add(dnsAddr);
            }
            String dns2 = this.mEthdns2;
            InetAddress dnsAddr2 = null;
            if (dns2.length() > 0) {
                dnsAddr2 = getIPv4Address(dns2);
                if (dnsAddr2 == null) {
                    Log.d(TAG,"wifi_ip_settings_invalid_dns2");
                }
                staticIpConfiguration.dnsServers.add(dnsAddr2);
            }
        }
        config.setIpConfiguration(
                new IpConfiguration(mIpAssignment, mProxySettings,
                        staticIpConfiguration, mHttpProxy));
        WifiManager.ActionListener mSaveListener = new WifiManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG,"mSaveListener : onSuccess");
            }
            @Override
            public void onFailure(int reason) {
                Log.i(TAG,"mSaveListener : onFailure : "+reason);
            }
        };
        Log.d(TAG,"mWifiManager.save");
        mWifiManager.save(config, mSaveListener);
        WifiManager.ActionListener mConnectListener = new WifiManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG,"mConnectListener : onSuccess");
            }
            @Override
            public void onFailure(int reason) {
                Log.i(TAG,"mConnectListener : onFailure : "+reason);
            }
        };
        Log.d(TAG,"mWifiManager.connect");
        if(this.isNew.equals("new")){
            mWifiManager.connect(config, mConnectListener);
            mWifiTracker.resumeScanning();
        }


    }

    private Inet4Address getIPv4Address(String text) {
        try {
            return (Inet4Address) NetworkUtils.numericToInetAddress(text);
        } catch (IllegalArgumentException | ClassCastException e) {
            return null;
        }
    }
}

然后修改 Settings 的Manifest
添加
<receiver android:name="com.android.setting.wifi.WifiBoardcastReceive"
                  android:exported="true">
                <intent-filter android:priority="1000">
                    <action android:name="android.net.action.WIFI_IP_CHANGED" />
                </intent-filter>
        </receiver>
即可实现通过广播进行设置ip及其他配置
后续其他设置的功能均可通过该方式实现

如何通过广播调用?

```java
Intent intent =new Intent();
        intent.setComponent(new ComponentName("com.android.settings","com.android.settings.wifi.WifiBoardcastReceive"));
        intent.setAction("android.net.action.WIFI_IP_CHANGED");
        Bundle bundle=new Bundle();
        bundle.putString("mMode","static");
        bundle.putString("ipaddr", "192.168.2.23");
        bundle.putString("netMask","255.255.255.2");
        bundle.putString("gateway","192.168.2.1");
        bundle.putString("dns1","4.4.4.4");
        bundle.putString("dns2","8.8.8.8");
        bundle.putString("SSID","CSUN-WLAN-4");
        bundle.putString("mPassword","Dw123456");
        bundle.putString("networkPrefix","24");
        bundle.putString("isNew","false");
//      bundle.putString("isNew","new");
        intent.putExtras(bundle);
        sendBroadcast(intent);
  • 写回答

1条回答 默认 最新

  • vivie_Y 2021-10-29 09:47
    关注

    您好,请问webrtc无摄像头播放远程摄像机视频,如何使用http方式呢,之前您的文章里写了,可是我这边双方通信后就没反应了,不显示视频,您知道是什么原因吗?谢谢!

    评论

报告相同问题?

问题事件

  • 创建了问题 10月28日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度