App crashes when using Settings.ACTION_WIFI_ADD_NETWORKS in combination with startActivityForResult

409 views Asked by At

I am trying to save network configurations with the following code on 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);
        }

I am debugging on Android 12 devices and face frequent app crashes (Not always) when saving new networks. Although my app crashes, the debugger is attached to vs2022. The target framework of my app is Android 12. I found some similar issues here: https://github.com/facebook/react-native/issues/36094 and https://community.oneplus.com/thread/1353760

Visual Studio Output

[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 studio 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
1

There are 1 answers

12
Liyun Zhang - MSFT On

I have created a new Xamarin.Android project to test your code.

The MainActivity:

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");
             };
        }
}

I tested both the StartActivity and StartActivityForResult method on the Android 12 emulator and the XiaoMi physical device with the Android 13. The network will be added successfully.

The result image:

enter image description here