Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult always returns 0 (RESULT_CANCELED) when calling settings

I might be approaching the problem in a wrong way..

What I'm doing is showing an alert message when there is no internet connection and the "ok" button leads the user to the wifi setting 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 is to reload the application or activity where it was.

For this I am doing the following call for the "ok" button:

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

}

on the activity's 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 gets called right after the "ok" button is pressed.

Should I approach this problem differently? How can I reload/refresh my app when the user comes back from the wifi settings?

I have checked similar answers but they seem to use an activity within the same app, so they can call set_result(...), but I can't do that here.

thanks!

like image 310
marimaf Avatar asked Sep 26 '12 17:09

marimaf


2 Answers

As others have said you should not call finish -- also calling startActivityForResult is not that helpful. Even if the resultCode was other than 0, it takes a few seconds to connect to WiFi usually.

So to answer your question, you should use a BroadcastReceiver and listen for network change intents.

This code is a bit old, but here is something similar I do in one of my apps. It should hopefully get you started.

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...
    }

}
like image 59
pjco Avatar answered Oct 16 '22 02:10

pjco


plesae don't use

          this.finish();

this will close your Activity completely. As you used startActivityForResult() method you should use this.finish after onActivityResult() method.

like image 23
Rajendra Avatar answered Oct 16 '22 03:10

Rajendra