onActivityResult总是返回0(RESULT_CANCELED)调用时设置 [英] onActivityResult always returns 0 (RESULT_CANCELED) when calling settings

查看:205
本文介绍了onActivityResult总是返回0(RESULT_CANCELED)调用时设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能是在一个错误的方式接近问题。

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

我在做什么是显示警告信息时,有没有互联网连接,并确定按钮,引导用户到WiFi的设置,打开互联网上。我想申请做,当用户改变(或没有)网络设置后,返回到它重新加载应用程序或活动在哪里。

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

但结果code始终为0,onActivityResult被确定按钮后,正确的叫法pressed。

but resultCode is always 0, onActivityResult gets called right after the "ok" button is pressed.

我应该解决这个问题不同?我如何可以重新加载/刷新我的应用程序,当用户回来的WiFi设置?

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

我已经检查了类似的答案,但他们似乎在同一个应用程序中使用的活动,这样他们就可以调用 set_result(...),但我做不到在这里。

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.

谢谢!

推荐答案

正如其他人说你不应该叫结束 - 也叫 startActivityForResult 是不是有帮助。即使结果code 为0以外,它需要几秒钟就可以连接到无线网络一般。

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.

因此​​,要回答你的问题,你应该使用的BroadcastReceiver 键,监听网络变更意图。

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

这code是有点老了,但这里是类似的东西我在我的应用程序之一。它会告诉你如何开始。

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

}

这篇关于onActivityResult总是返回0(RESULT_CANCELED)调用时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆