Android 持有线程时创建AlertDialog

Android 持有线程时创建AlertDialog,android,multithreading,dialog,Android,Multithreading,Dialog,我对android这个东西还很陌生,所以我仍然对一些概念感到困惑。我想做的是,当我在启动屏幕上时,当用户未连接到internet时,显示一个AlertDialog。我已经尝试了很多方法,但是我永远都抓不住线程,所以我总是得到一个异常,因为活动在对话框关闭之前就关闭了。我试着和一个训练员一起做,但没有成功。。。我的部分代码如下: public class Splash extends Activity { public void openDialog() { AlertDialog.

我对android这个东西还很陌生,所以我仍然对一些概念感到困惑。我想做的是,当我在启动屏幕上时,当用户未连接到internet时,显示一个AlertDialog。我已经尝试了很多方法,但是我永远都抓不住线程,所以我总是得到一个异常,因为活动在对话框关闭之前就关闭了。我试着和一个训练员一起做,但没有成功。。。我的部分代码如下:

public class Splash extends Activity {

 public void openDialog() {
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

        // set title
        alertDialogBuilder.setTitle(R.string.app_name);

        // set dialog message
        alertDialogBuilder
            .setMessage(R.string.dialogo_net)
            .setCancelable(false)
            .setPositiveButton("Sair",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Splash.this.finish();
                }
              })
            .setNegativeButton("Definições",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Intent intent = new Intent(Settings.ACTION_SETTINGS);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);           
                    startActivity(intent);
                }
            });

            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();
        }

 Handler mHandler = new Handler()
 {
     public void handleMessage(Message msg)
     {
        openDialog();//Display Alert
     }
 };

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


    Thread logoTimer = new Thread() {
        public void run(){
            try{
                int logoTimer = 0;
                while(logoTimer < 2000){
                    sleep(100);
                    logoTimer = logoTimer +100;
                };
                ConnectivityManager cm =
                     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                 NetworkInfo netInfo = cm.getActiveNetworkInfo();
           if (netInfo == null || !netInfo.isConnectedOrConnecting()) {
               //opens the dialog in this case
            mHandler.sendEmptyMessage(0);
              } else {
                //goes to main activity
                startActivity(new Intent("pt.aeist.mobile.START")); }
            } 

            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            finally{
                finish();
            }
        }
    };

    logoTimer.start();
公共类启动扩展活动{
公共void openDialog(){
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(
这),;
//定名
alertDialogBuilder.setTitle(R.string.app_name);
//设置对话框消息
alertDialogBuilder
.setMessage(R.string.dialogo_net)
.setCancelable(错误)
.setPositiveButton(“Sair”,新的DialogInterface.OnClickListener()对话框){
public void onClick(DialogInterface对话框,int-id){
Splash.this.finish();
}
})
.setNegativeButton(“Definições”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
意向意向=新意向(设置、动作和设置);
intent.addCategory(intent.CATEGORY_启动器);
星触觉(意向);
}
});
AlertDialog AlertDialog=alertDialogBuilder.create();
alertDialog.show();
}
Handler mHandler=新处理程序()
{
公共无效handleMessage(消息消息消息)
{
openDialog();//显示警报
}
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
线程logoTimer=新线程(){
公开募捐{
试一试{
int logoTimer=0;
而(logoTimer<2000){
睡眠(100);
logoTimer=logoTimer+100;
};
连接管理器cm=
(ConnectionManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo netInfo=cm.getActiveNetworkInfo();
如果(netInfo==null | |!netInfo.isConnectedOrConnecting()){
//在这种情况下打开对话框
mHandler.sendEmptyMessage(0);
}否则{
//进入主要活动
startActivity(新意图(“pt.aeist.mobile.START”);}
} 
捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
最后{
完成();
}
}
};
logoTimer.start();
}

}

//单独处理finish()

.setNegativeButton("Definições",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);           
                startActivity(intent);
                finish();
            }
        });
只需从所有位置移除finish()。检查一下


您必须确定要放置finish()的情况

它仍然会立即显示,然后消失,例外情况是窗口泄漏
.setNegativeButton("Definições",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);           
                startActivity(intent);
                finish();
            }
        });