这是真的讨厌我。我有一个使用GCM的应用程序。我的人抱怨他们不能由于一个错误登录。让我告诉你我是如何做的登录:
This is really annoying me. I have an app that uses GCM. I have people complaining they can't login due to an error. Let me show you how I am making the Login:
// Login Activity
//....
public void onClickLoginButton (View v) {
// Do some stuff...
new GCMRegister().execute(); //AsyncTask
}
private class GCMRegister extends AsyncTask<Void, Void, Integer> {
@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失败上注册。即使是我自己和我的设备,安卓4.4.2,不能在第一个做到这一点。我需要尝试登录2〜3次,直到它的工作原理(和Im在一个良好的连接)。我的LogCat中的错误是:
So a lot of people is complaining they can't login in because of "A problem occured login" error, which indicates that GCM is failing on register. Even myself with my device, Android 4.4.2, can't do it at first. I need to try to login 2 or 3 times until it works (and Im in a good connection). The error on my LogCat is:
08-04 21:29:10.922: E/GCMRegister(18182): java.io.IOException: SERVICE_NOT_AVAILABLE
那么,什么是错我的code?这是推动我疯了。
So what is wrong with my code? This is driving me nuts.
我们面临着同样的问题,用户无法登陆的第一次,但可以登录后说的第2或第3次不考虑网速。
We faced the same issue, where users can't login the first time but can login after say the 2nd or 3rd time irrespective of the network speed.
我们用做一种变通方法视频下载
,然后重试了许多次,直到GCM ID是:收到。
We did a workaround by using Thread.sleep
and retry for a number of times, till the GCM ID is recieved.
int noOfAttemptsAllowed = 5; // Number of Retries allowed
int noOfAttempts = 0; // Number of tries done
bool stopFetching = false; // Flag to denote if it has to be retried or not
String regId = "";
while (!stopFetching)
{
noOfAttempts ++;
GCMRegistrar.register(getApplicationContext(), "XXXX_SOME_KEY_XXXX");
try
{
// Leave some time here for the register to be
// registered before going to the next line
Thread.sleep(2000); // Set this timing based on trial.
} catch (InterruptedException e) {
e.printStackTrace();
}
try
{
// Get the registration ID
regId = GCMRegistrar.getRegistrationId(LoginActivity.this);
} catch (Exception e) {}
if (!regId.isEmpty() || noOfAttempts > noOfAttemptsAllowed)
{
// If registration ID obtained or No Of tries exceeded, stop fetching
stopFetching = true;
}
if (!regId.isEmpty())
{
// If registration ID Obtained, save to shared preferences
saveRegIDToSharedPreferences();
}
}
注意:的视频下载
和 noOfAttemptsAllowed
可与围绕发挥你的设计等参数。我们有7000睡眠时间,以便得到注册在第一次尝试的概率较高。然而,如果失败,则下一次尝试将消耗另一个7000ms。这可能会导致用户认为您的应用程序很慢。所以,聪明玩弄这两个值。
Note: The Thread.sleep
and noOfAttemptsAllowed
can be played around with based on your design and other parameters. We had a sleep time of 7000 so that probability of getting registered at first attempt is higher. However, if it fails, the next attempt would consume another 7000ms. This might cause users to think your app is slow. So, play around intelligently with those two values.
上一篇:Android的 - 服务始终保持运行始终保持、Android
下一篇:一次编写在Windows Mobile 6,Windows Phone 7的,Android和iPhone部署?写在、Windows、Mobile、iPhone