OnActivityResult always returns 0 (RESULT_CANCELED) when calling settings

I could get the problem wrong.

What I am doing is showing a warning message when there is no Internet connection, and the ok button leads the user to configure Wi-Fi to turn on the Internet. What I want the application to do when the user returns to it after changing (or not) the Internet settings, restarts the application or activity where it was.

To do this, I make the following call to the ok button:

 static void startAct(Activity ctxt)
{
    ctxt.startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), WIFI_SETTINGS);

}

in the activity class where this message is shown, I have the following:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == WIFI_SETTINGS && resultCode == RESULT_OK) 
     {
         this.finish();

         Intent myIntent = new Intent(this, MyActivity.class);
         startActivity(myIntent);
     }
}

but resultCode is always 0, onActivityResult is called immediately after clicking the "ok" button.

-? / , Wi-Fi?

, , , , set_result(...), .

!

+5
3

, - startActivityForResult . resultCode 0, WiFi .

, , BroadcastReceiver .

, - . , .

protected void registerWifiReceivers()
{   
    IntentFilter f1 = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    IntentFilter f2 = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION;
    this.registerReceiver(mReceiver, f1);
    this.registerReceiver(mReceiver, f2);       
}



final BroadcastReceiver mReceiver = new BroadcastReceiver() 
{       
    @Override
    public void onReceive(Context context, Intent intent) 
    {   
      String action = intent.getAction();        
      Log.d ( TAG, "BroadcastReceiver: " + action );

      if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
      {
         Log.i ( TAG, "handling event: WifiManager.NETWORK_STATE_CHANGED_ACTION action: "+action );
         handleWifiStateChange(intent);
      }
      else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) 
      {   
         Log.i ( TAG, "ignoring event: WifiManager.WIFI_STATE_CHANGED_ACTION action: "+action );
      } 
    }
}

protected void handleWifiStateChange ( Intent intent )
{   
    NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);      
    if (info.getState().equals(NetworkInfo.State.CONNECTED))
    {
        //do something...
    }

}
+2

plesae

          this.finish();

. startActivityForResult(), this.finish onActivityResult().

+1

Do not create a new intention. Just do it -

finish();
startActivity(getIntent());
0
source

All Articles