2013-05-28 64 views
0

我使用此脚本获取注册ID doInbackground AsyncTask,但注册ID仍为空。没有错误或强制关闭,但注册ID为空。使用asynctask获取GCM注册ID

public class RegisterTask extends AsyncTask<String, Void, Integer> { 

public RegisterTask(RegisterActivity activity, ProgressDialog progressDialog) { 
    this.activity = activity; 
    this.progressDialog = progressDialog; 
} 

@Override 
protected void onPreExecute() { 
    progressDialog.show(); 
} 

@Override 
protected Integer doInBackground(String... arg0) { 
    inputFullName = (EditText) activity.findViewById(R.id.registerName); 
    inputEmail = (EditText) activity.findViewById(R.id.registerEmail); 
    inputPassword = (EditText) activity.findViewById(R.id.registerPassword); 
    String name = inputFullName.getText().toString(); 
    String email = inputEmail.getText().toString(); 
    String password = inputPassword.getText().toString(); 
    Log.v(email, password); 
    UserFunctions userFunction = new UserFunctions(); 

    // Make sure the device has the proper dependencies. 
    GCMRegistrar.checkDevice(activity); 

    // Make sure the manifest was properly set - comment out this line 
    // while developing the app, then uncomment it when it's ready. 
    GCMRegistrar.checkManifest(activity); 

    // lblMessage = (TextView) findViewById(R.id.lblMessage); 

    activity.registerReceiver(activity.mHandleMessageReceiver, 
      new IntentFilter(DISPLAY_MESSAGE_ACTION)); 
    // Get GCM registration id 
    regId = GCMRegistrar.getRegistrationId(activity); 

    JSONObject json = userFunction.registerUser(name, email, password, 
      regId); 
    // check for login response 
    try { 
     if (json.getString(KEY_SUCCESS) != null) { 
      // registerErrorMsg.setText(""); 
      String res = json.getString(KEY_SUCCESS); 
      if (Integer.parseInt(res) == 1) { 
       // user successfully registred 
       // Store user details in SQLite Database 
       DatabaseHandler db = new DatabaseHandler(
         activity.getApplicationContext()); 
       JSONObject json_user = json.getJSONObject("user"); 

       // Clear all previous data in database 
       userFunction.logoutUser(activity.getApplicationContext()); 
       db.addUser(json_user.getString(KEY_ID_USER), 
         json_user.getString(KEY_NAMA), 
         json_user.getString(KEY_EMAIL), 
         json_user.getString(KEY_REGID), 
         json_user.getString(KEY_JKEL), 
         json_user.getString(KEY_TLAHIR), 
         json_user.getString(KEY_INSTANSI), 
         json_user.getString(KEY_JABATAN), 
         json_user.getString(KEY_DIBUAT_AT), 
         json_user.getString(kEY_AVATAR), 
         json_user.getString(kEY_STATUS)); 
       // successful registration 
       responseCode = 1; 
      } else { 
       // Error in registration 
       responseCode = 0; 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return responseCode; 
} 

@Override 
protected void onPostExecute(Integer responseCode) { 
    EditText userName = (EditText) activity 
      .findViewById(R.id.registerEmail); 
    EditText passwordEdit = (EditText) activity 
      .findViewById(R.id.registerPassword); 
    String s = userName.getText().toString(); 

    if (responseCode == 1) { 
     progressDialog.dismiss(); 
     activity.registerReport(responseCode); 
     userName.setText(""); 
     passwordEdit.setText(""); 
    } 
    if (responseCode == 0) { 
     progressDialog.dismiss(); 
     activity.registerReport(responseCode); 
    } 
} 
} 

请帮助

+0

这不是它的工作方式,如果你不使用'GCMRegistrar.register(Context)'登录第一个GCM服务,它将永远是空的。 [阅读Google Cloud M.文档](http://developer.android.com/google/gcm/gs.html) – AlexBcn

+0

这是[此问题]的副本(http://stackoverflow.com/questions/ 16345292/GCM-REG-ID-是空的)。 – Eran

回答

0

你必须做,以获取使用GCMRegistrar.register(mContext)的ID。您必须在清单文件中声明一个接收器来启用接收注册和消息意图。现在要真正收到消息,您必须实施自己的服务,以扩展GCMBaseIntentService。 Here你可以找到GCM的教程。

+0

可以把GCMRegistrar.register(mContext)放在doinbackground中吗? final String regId = GCMRegistrar.getRegistrationId(activity); \t \t如果(regId.equals( “”)){ \t \t \t //登记是不存在,现在用GCM注册\t \t \t \t \t \t //GCMRegistrar.register(this,SENDER_ID); \t \t \t GCMRegistrar.register(activity,SENDER_ID); \t \t} –