Android: Back to App after setting wifi

蓝咒 提交于 2020-01-25 05:05:08

问题


In myActivity, This is my code to check if my phone connect to the Internet or not.

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

And to the resume:

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

My problem is, when I've done setting wifi connection and press back button. It back to the menu screen of my phone, not resume myActivity. Only if I launch my app again after that, the onResume() function is executed.
So, what's my missing to back to my app after setting wifi?


回答1:


this is your answer I think , you have to open the settings as ActivityForResult :

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

You can implement it in an AlertDialog like this:

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


来源:https://stackoverflow.com/questions/22882479/android-back-to-app-after-setting-wifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!