安卓:返回应用程序设置后,无线网络连接(Android: Back to App after set

2019-10-19 18:28发布

在myActivity,这是我的代码检查我的手机连接到互联网或没有。

if (!isConnected()) {
            // super.playingVideo.setVideoUrl(product.getVideoUrl());
            String message = getResources().getString(R.string.wifi_prompt);
            super.showDialog(this, message, R.string.wifi_setting,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            if (dialog != null) {
                                dialog.dismiss();
                            }
                            currentProduct = product;
                            isViewRequest = true;
                            startActivity(new Intent(
                                    android.provider.Settings.ACTION_WIRELESS_SETTINGS));

                        }
                    }, R.string.back, dismissDialogListener);
        } else {...}

而到了简历:

@Override
protected void onResume() {
    // After setting wifi
    if (isViewRequest) {
        ...//mycode
    }
    super.onResume();
}

我的问题是,当我做了设置无线网络连接,然后按返回键。 这回我的电话的菜单画面,不会恢复myActivity。 只有当我之后再次启动我的应用程序,执行的onResume()函数。
那么,什么是我缺少的wifi设置后,回到我的应用程序?

Answer 1:

这是你的答案,我想,你必须打开设置为ActivityForResult:

startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0);

您可以在实现它AlertDialog是这样的:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                this);

        // Setting Dialog Title
        alertDialog.setTitle("Confirm...");

        // Setting Dialog Message
        alertDialog.setMessage("Do you want to go to wifi settings?");

        // Setting Icon to Dialog
        // alertDialog.setIcon(R.drawable.ic_launcher);

        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        // Activity transfer to wifi settings
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS), 0);
                    }
                });

        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("no",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Write your code here to invoke NO event

                        dialog.cancel();
                    }
                });

        // Showing Alert Message
        alertDialog.show();


文章来源: Android: Back to App after setting wifi