这真让我讨厌.我有一个使用GCM的应用程序.我有人抱怨他们因错误而无法登录.让我告诉你我是如何进行登录的:
// Login Activity
//....
public void onClickLoginButton (View v) {
// Do some stuff...
new GCMRegister().execute(); //AsyncTask
}
private class GCMRegister extends AsyncTask {
@Override
protected Integer doInBackground (Void... params) {
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
try {
//Get GCM Registration key
registrationId = googleCloud.register(Settings.PROJECT_NUMBER);
} catch (Exception e) {
Log.e("GCMRegister", e.toString());
return -1;
}
return 1;
} else {
return -3;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
new LoginAsync().execute(); // Another AsyncTask to check in my database if user is valid and save its key
} else {
user.setEnabled(true);
password.setEnabled(true);
login.setEnabled(true);
if (result == -1) {
Toast.makeText(Login.this, "A problem occured login in", Toast.LENGTH_LONG).show();
} else if (result == -3) {
Toast.makeText(Login.this, "I need an internet connection", Toast.LENGTH_LONG).show();
}
}
}
所以很多人抱怨他们因为"登陆时出现问题"错误而无法登录,这表明GCM登记失败.即使我自己使用我的设备Android 4.4.2,也不能一开始就做到这一点.我需要尝试登录2到3次,直到它工作(并且我的连接很好).我的LogCat上的错误是:
08-04 21:29:10.922: E/GCMRegister(18182): java.io.IOException: SERVICE_NOT_AVAILABLE
那我的代码有什么问题?这让我疯了.