如何使用AlertDialog导航到下一个活动?

时间:2014-02-05 14:39:35

标签: android

我遇到问题在哪里添加intent和finish();在alertdialog。

此时,当用户有互联网连接时,系统会弹出一个提示,并在alertdialog框中显示您有互联网连接,当用户点击确定或继续按钮时,他必须继续下一个活动。 如果用户没有互联网连接,则会弹出警告并在警告对话框中显示您没有互联网连接,当用户点击退出按钮应用程序将关闭。

AndroidDetectInternetConnectionActivity.java中的代码

package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity extends Activity {

// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnStatus = (Button) findViewById(R.id.btn_check);

    // creating connection detector class instance
    cd = new ConnectionDetector(getApplicationContext());

    /**
     * Check Internet status button click event
     * */
    btnStatus.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // get Internet status
            isInternetPresent = cd.isConnectingToInternet();

            // check for Internet status
            if (isInternetPresent) {
                // Internet Connection is Present
                // make HTTP requests
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                        "You have internet connection", true);
//Here user has to continue next activity
            } else {
                // Internet connection is not present
                // Ask user to connect to Internet
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                        "You don't have internet connection.", false);
//here application should close
            }
        }

    });

}

/**
 * Function to display simple Alert Dialog
 * @param context - application context
 * @param title - alert dialog title
 * @param message - alert message
 * @param status - success/failure (used to set icon)
 * */
public void showAlertDialog(Context context, String title, String message, Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
    alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);


    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

}

ConnectionDetector,java

中的代码
package com.example.detectinternetconnection;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}
}

那么告诉我在哪里投入进行下一个活动并完成();关闭申请。

谢谢...

2 个答案:

答案 0 :(得分:1)

使用showAlertDialog最后一个参数status根据互联网连接状态执行Ok按钮点击操作:

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

           if(status){
             // cancel Alert Dialog
             // Move to Activity
           }else{
             // cancel Alert Dialog
             // close Activity
           }

    }
});

答案 1 :(得分:0)

单击警告对话框确定是否存在Internet。

 alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
            if(isInternetPresent){
           Intent i=new Intent(Activity.this,targetActivity.class);
        startActivity(i);
            }else{
             //code for exit here it send to home screen
      Intent startMain = new Intent(Intent.ACTION_MAIN);
                      startMain.addCategory(Intent.CATEGORY_HOME);
                      startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      startActivity(startMain);
    }
            }
            });