结合使用 Settings.ACTION_WIFI_ADD_NETWORKS 和 startActivityForResult 时应用程序崩溃

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

我正在尝试在 xamarin Android 上使用以下代码保存网络配置。

   private void AddWifi(string ssid, string psk)
        {
            var suggestions = new List<IParcelable>
            {
                new WifiNetworkSuggestion.Builder()
                        .SetSsid(ssid)
                        .SetWpa2Passphrase(psk)
                        .Build()
            };
            var bundle = new Bundle();
            bundle.PutParcelableArrayList(ExtraWifiNetworkList, suggestions);
            var intent = new Intent(ActionWifiAddNetworks);
            intent.PutExtras(bundle);
            MainActivity.Instance.StartActivityForResult(intent, RequestCodeConstants.WifiRequestCode);
        }

我在 Android 12 设备上进行调试,在保存新网络时面临频繁的应用程序崩溃(并非总是如此)。虽然我的应用程序崩溃了,但调试器已附加到 vs2022。我的应用程序的目标框架是 Android 12。 我在这里发现了一些类似的问题:https://github.com/facebook/react-native/issues/36094https://community.oneplus.com/thread/1353760

Visual Studio 输出

[DecorView[]] onWindowFocusChanged hasWindowFocus false
[tWriterEnhance] Explicit concurrent copying GC freed 119792(5287KB) AllocSpace objects, 5(164KB) LOS objects, 24% free, 5230KB/6974KB, paused 91us,85us total 54.456ms
[Activity] PerfMonitor: Slow Operation: Activity com.packagename/crc647e3ada291cd56112.MainActivity onDestroy took 627ms
[Choreographer] Skipped 61 frames!  The application may be doing too much work on its main thread.
[Looper] PerfMonitor doFrame : time=0ms vsyncFrame=0 latency=683ms procState=-1 historyMsgCount=2 (msgIndex=2 wall=671ms seq=47927 late=20ms h=android.app.ActivityThread$H w=159)

Android 工作室 Logcat

getRecentTasks: taskId=925   userId=0   baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.packagename/crc647e3ada291cd56112.MainActivity }
2023-03-09 21:50:10.680  1784-12912 ActivityTaskManager     system_server                        W    Force finishing activity com.packagename/crc647e3ada291cd56112.MainActivity
2023-03-09 21:50:11.859  7578-7578  tWriterEnhance          com...Inc.packagename  I  Explicit concurrent copying GC freed 119792(5287KB) AllocSpace objects, 5(164KB) LOS objects, 24% free, 5230KB/6974KB, paused 91us,85us total 54.456ms
2023-03-09 21:50:11.986  7578-7578  Activity                com...Inc.packagename  W  PerfMonitor: Slow Operation: Activity com.packagename/crc647e3ada291cd56112.MainActivity onDestroy took 627ms
2023-03-09 21:50:11.997  1784-12916 InputManager-JNI        system_server                        W  Input channel object '22029a1 com.packagename/crc647e3ada291cd56112.MainActivity (client)' was disposed without first being removed with the input manager!
2023-03-09 21:50:12.008  7578-7578  Choreographer           com...Inc.packagename  I  Skipped 61 frames!  The application may be doing too much work on its main thread.
2023-03-09 21:50:12.009  7578-7578  Looper                  com...Inc.packagename  W  PerfMonitor doFrame : time=0ms vsyncFrame=0 latency=683ms procState=-1 historyMsgCount=2 (msgIndex=2 wall=671ms seq=47927 late=20ms h=android.app.ActivityThread$H w=159)
2023-03-09 21:50:16.005  3068-3393  ActivityManagerWrapper  com.miui.home                        E  getRecentTasks: taskId=925   userId=0   baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.packagename
android xamarin xamarin.android android-10.0
1个回答
0
投票

我创建了一个新的 Xamarin.Android 项目来测试您的代码。

主要活动:

public class MainActivity : AppCompatActivity
{
        private void AddWifi(string ssid, string psk)
        {
            var suggestions = new List<IParcelable>
            {
                new WifiNetworkSuggestion.Builder()
                        .SetSsid(ssid)
                        .SetWpa2Passphrase(psk)
                        .Build()
            };
            var bundle = new Bundle();
            bundle.PutParcelableArrayList(Android.Provider.Settings.ExtraWifiNetworkList, suggestions);
            var intent = new Intent(Android.Provider.Settings.ActionWifiAddNetworks);
            intent.PutExtras(bundle);
       //   this.StartActivity(intent);
            this.StartActivityForResult(intent,0); 
        }
       
        protected override void OnCreate(Bundle savedInstanceState)
        {
              base.OnCreate(savedInstanceState);

              // Create your application here
              SetContentView(Resource.Layout.activity_main);
              var button = FindViewById<Button>(Resource.Id.button1);
              button.Click += delegate
              {
                  AddWifi("test123456", "test123456");
             };
        }
}

我在Android 12模拟器和小米实体机上用Android 13分别测试了

StartActivity
StartActivityForResult
方法,都会添加网络成功

结果图:

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