Skip to content

Instantly share code, notes, and snippets.

@danylovolokh
Last active January 3, 2016 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danylovolokh/7b95dfe367b280be89e5 to your computer and use it in GitHub Desktop.
Save danylovolokh/7b95dfe367b280be89e5 to your computer and use it in GitHub Desktop.
public class LoginActivity extends AppCompatActivity {
//... some fields here
private BroadcastReceiver mLoginBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mLogin = (TextView) findViewById(R.id.login);
mPassword = (TextView) findViewById(R.id.password);
Button performLoginButton = (Button) findViewById(R.id.perform_login);
performLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String login = mLogin.getText().toString();
String password = mPassword.getText().toString();
performLoginViaService(login, password);
}
});
}
private void performLoginViaService(String login, String password) {
mLoginBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
mLoginBroadcastReceiver = null;
LoginResponse loginResponse = LoginResponse.getLoginResponse(intent);
handleLoginResponse(loginResponse);
}
};
registerReceiver(mLoginBroadcastReceiver, new IntentFilter(LOGIN_RECEIVER_FILTER));
LoginService.performAsyncLogin(this, LOGIN_RECEIVER_FILTER, login, password);
}
/**
* This is a method that will be overridden in order to test response.
*/
@VisibleForTesting
public void handleLoginResponse(LoginResponse loginResponse) {
// handle login response here
}
// some code here
}
@danylovolokh
Copy link
Author

This is a LoginActivity that perform simple login with a help of service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment