以下代码:
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方法
答案 0 :(得分:1)
只需替换
execute(null);
与
execute(); or execute("url");
答案 1 :(得分:0)
避免在Toast
中使用doInBackground()
...因为它在后台线程上运行。而是使用Log
检查值。