1

下面给出的代码:

private void registerBackground(){

        Toast toast3 = Toast.makeText(context,"-----in regbackground class---------",Toast.LENGTH_LONG);
        toast3.show();


     new AsyncTask<Void,Void,String>(){

             @Override
             protected String doInBackground(Void... params) {

                String msg = "";

                Toast toast = Toast.makeText(context, "---out of try ---"+msg, Toast.LENGTH_LONG);
                toast.show();

                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(context);
                  }
                    regid = gcm.register(SENDER_ID);

                    Toast toast1 = Toast.makeText(context, "---regid ---"+regid+"----from background---toast 1", Toast.LENGTH_LONG);
                    toast1.show();

                    msg = "Device registered, registration id=" + regid;

                    Toast toast2 = Toast.makeText(context, "---regid ---"+msg+"----from background---toast 2", Toast.LENGTH_LONG);
                    toast2.show();
                    setRegistrationId(context, regid);


                } catch (IOException ex) {
                    msg = "Error ::" + ex.getMessage();
                    Toast toast4 = Toast.makeText(context,msg+"---error msg",Toast.LENGTH_LONG);
                    toast4.show();
                }
                return msg;

            }


            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            Toast toast5 = Toast.makeText(context,msg+"---on post execute",Toast.LENGTH_LONG);
            toast5.show();
            }
        }.execute(null);



}

这是我的整个代码。问题是唯一的 toast3 正在执行之后没有执行 Asynctask 方法

4

2 回答 2

1

只需更换

execute(null);

execute(); or execute("url");
于 2013-09-03T11:18:36.263 回答
0

避免使用Toastin doInBackground()... 因为它在后台线程上运行。而是用于Log检查值。

于 2013-09-03T11:29:20.580 回答