Allowing Service Time To Start Up
I wish to enable automatic login to my app, yet the problem that is arising is that the service is being started and bound after the automatic login features are being called: 04-
Solution 1:
I posted the auto login code within the ServiceConnection
block. The issue was that the service is connected only after the onCreate block, therefore it was always NULL when calling anything against it.
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
imService = ((MessagingService.IMBinder) service).getService();
Log.v("Service", "The service is connected");
// Allowing autologin!!**********************************************************
final String userName = SaveSharedPreference.getUserName(getApplicationContext());
final String password = SaveSharedPreference.getPassword(getApplicationContext());
Log.v("TEST GCM Service Login", "SharedPref Username is " + userName + " and password " + password);
//Log.v("TEST GCM Service Login", "The value of imService is: " + imService.toString());
if (imService == null) {
Log.v("TEST GCM Service Login", "Service is NULL");
Toast.makeText(getApplicationContext(),
R.string.not_connected_to_service,
Toast.LENGTH_LONG).show();
// showDialog(NOT_CONNECTED_TO_SERVICE);
//return;
} else if (imService.isNetworkConnected() == false) {
Log.v("TEST GCM Service Login", "imService.isNetworkConnected() == false");
Toast.makeText(getApplicationContext(),
R.string.not_connected_to_network,
Toast.LENGTH_LONG).show();
// showDialog(NOT_CONNECTED_TO_NETWORK);
} else {
Log.v("TEST GCM Service Login", "When imService is not null: SharedPref Username is " + userName + " and password " + password);
Thread autoLoginThread = new Thread() {
private Handler handler = new Handler();
@Override
public void run() {
String result = null;
try {
result = imService.authenticateUser(
userName.trim(),
password.trim());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (result == null
|| result.equals(AUTHENTICATION_FAILED)) {
/*
* Authenticatin failed, inform the user
*/
handler.post(new Runnable() {
public void run() {
Toast.makeText(
getApplicationContext(),
R.string.make_sure_username_and_password_correct,
Toast.LENGTH_LONG).show();
// showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
}
});
} else { /*
* if result not equal to authentication failed,
* result is equal to friend and group list of
* the user 0: is for friends, 1: is for groups
*/
handler.post(new Runnable() {
public void run() {
Intent i = new Intent(LoggingIn.this,
MainActivity.class);
startActivity(i);
LoggingIn.this.finish();
}
});
}
}
};
autoLoginThread.start();
}
if (imService.isUserAuthenticated() == true) {
// Intent i = new Intent(LoggingIn.this, ListOfFriends.class);
Intent i = new Intent(LoggingIn.this, MainActivity.class);
startActivity(i);
LoggingIn.this.finish();
}
}
public void onServiceDisconnected(ComponentName className) {
Log.v("Service", "The service is disconnected");
imService = null;
Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
Toast.LENGTH_SHORT).show();
}
};
Post a Comment for "Allowing Service Time To Start Up"