0

在给定的活动中,AlertDialog 将用户带入 WiFI 设置。然后,用户按下返回按钮以返回到所述活动。

但是,一旦按下后退按钮,我就需要进行方法调用。请注意,我不能简单地在活动中的以下代码之后添加该方法,因为这会影响用户必须与 AlertDialog 实例交互的时间。

方法调用需要在 WIFI 设置菜单中按下后退按钮后立即进行。请告诉我如何实现这一点。

这是代码:

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
          Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
          startActivity(intent);
         }
     });
4

3 回答 3

1

class member

private static final int WIFI_REQUEST = 1234;

Use startActivityForResult

alertDialog.setPositiveButton("Settings", new dialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
      startActivityForResult(intent, WIFI_REQUEST);
     }
 });  

In the activity class

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode)
    {
         case WIFI_REQUEST:
              // Call your method here
              break;
    }
}
于 2013-03-23T02:07:07.313 回答
1

You can Override the onResume() method of the calling Activity. As soon soon as the user presses the "back" button the onResume() method is sure to get called so you should be able to put your method call here

于 2013-03-23T02:10:08.110 回答
0
private boolean inwifisettings;

public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
    inwifisettings = true;
    startActivity(intent);
}

@Override public void onWindowFocusChanged(boolean hasFocus)
{
    if(inwifisettings & hasFocus)
    {
         doSomething();
         inwifisettings = false;
    }
}

您不应为此目的使用 onResume() 或 startActivityForResult()/onActivityResult()。引用 Android 文档:http: //developer.android.com/reference/android/app/Activity.html

public void startActivityForResult (Intent intent, int requestCode, Bundle options)
请注意,此方法只能与定义为返回结果的 Intent 协议一起使用。在其他协议中(例如 ACTION_MAIN 或 ACTION_VIEW),您可能无法得到预期的结果。例如,如果您正在启动的活动使用 singleTask 启动模式,它将不会在您的任务中运行,因此您将立即收到取消结果。

public void onWindowFocusChanged (boolean hasFocus)
This is the best indicator of whether this activity is visible to the user.
the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

于 2013-03-23T02:01:16.403 回答