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!
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...
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With