如何留在一个活动,如果Internet连接的asynchorous任务之间失去了 [英] How to stay in an activity if the Internet connection is lost in between an asynchorous task

查看:101
本文介绍了如何留在一个活动,如果Internet连接的asynchorous任务之间失去了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动的按钮。当我preSS它,它就会加载另一个活动。在此活动我有在每个小区的描述和图像的自定义网格视图。我填充通过异步任务从远程服务器网格视图:

I have a button in an activity. When I press it, it will load another activity. In this activity I have a custom grid view with a description and an image in each cell. I am populating the grid view from a remote server through an asynchronous task:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        if (isInternetAvailable) {
            Intent intent = new Intent(menuActivity.this,
                    gridActivity.class);
            startActivity(intent);
        }
        else {
            MyAlertDialog.ShowAlertDialog(menuActivity.this,
                                          "",
                                          "No network connection",
                                          "OK");
        }
    }
});

我是()这里分享testAsyncTask。我在gridActivity的OnCreate()(第二项活动)

I am sharing the testAsyncTask() here. I am calling this function in the onCreate() of gridActivity (the second activity)

private void testAsyncTask() {
    new AsyncTask<Object, Object, Object>() {

        @Override
        protected void onPreExecute() {

            progress_Dialog = ProgressDialog.show(a, "", "Loading");
        }

        @Override
        protected Integer doInBackground(Object... params) {
            try {
                MenuService menuService = new MenuServiceImpl();
                MenuServiceResponse pMenu = menuService.getMenu();
                itemlist = pMenu.getMenu().getMenus();
                return 0;
            }
            catch (MyServiceException e) {
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                e.printStackTrace();
            }
            return 0;
        }

        @Override
        protected void onPostExecute(Object result) {

            if (progress_Dialog != null) {
                progress_Dialog.dismiss();
            }

            try
            {
                adapter = new GridAdapter(
                    GridActivity.this, itemlist);
                AllitemgridView.setAdapter(adapter);

            }
            catch(Exception e)
            {
                adapter = null;
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "Check Network Connection", "OK");
            }

        }
    }.execute();
}

我检查的第一项活动的条件,我不停的按钮。我将会触发启动activty只有当我有一个互联网连接。

I am checking a condition in the first activity where I kept the button. I will fire the start activty only if I have an Internet connection.

现在在我的问题,如果我有一个互联网连接和I preSS按钮,startacvitiy会着火,它会移动到第二个活动。在第二个活动的onCreate()我打电话testAsync()。现在,它会显示一个加载对话框,并在后台,我会从远程服务器获取数据。

Now in to my problem, if I have an Internet connection and I press the button, startacvitiy will fire, and it will move to the second activity. In the onCreate() of the second activity I'm calling the testAsync(). Now it will show a loading dialog box and in the background I will fetch the data from a remote server.

现在,如果将我的调制解调器关闭,应用程序冻结了5-6秒,然后它会强制关闭。那么,如何处理这个异常?

Now if I switch my modem off, the application freezes for 5-6 seconds and then it will force close. So how can I handle this exception?

在doInbackgorund()内的编码需要一个互联网连接,这可能是因为当我拔下此崩溃的原因
从Wi-Fi。我该如何处理这个崩溃?

The coding inside the doInbackgorund() requires an Internet connection and that could be the reason for this crash when I disconnect from Wi-Fi. How can I handle this crash?

推荐答案

您不能在doInBackground方法使用,因为这种方法是做另一个线程,
MyAlertDialog.ShowAlertDialog(GridActivity.this,,无网络连接检测,OK);

You can't use this in doinbackground method, because this method is doing another thread, MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");

您只能在onPOST等方法使用它。

You can only use it in the onpost method.

或者你也可以使用对话框消息:

Or you can use the dialog message:

    @Override
    protected Integer doInBackground(Object... params) {
        try {
            MenuService menuService = new MenuServiceImpl();
            MenuServiceResponse pMenu = menuService.getMenu();
            itemlist = pMenu.getMenu().getMenus();
            return 0;
        } catch (MyServiceException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                }
            });
            e.printStackTrace();
        }
    }

这篇关于如何留在一个活动,如果Internet连接的asynchorous任务之间失去了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆