Intent registration / login service in the application.
@Override protected void onHandleIntent(Intent intent) { try { // ЗАЧЕМ ? synchronized (TAG) { // [START register_for_gcm] // Initially this call goes out to the network to retrieve the token, subsequent calls // are local. // [START get_token] InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken( getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); // [END get_token] if (intent.getStringExtra("state").equals("registration")) { //make registration request with callback } else if (intent.getStringExtra("state").equals("login")) { //make login request with callback } subscribeTopics(token); // [END register_for_gcm] } } catch (Exception e) { } } Questions:
- Why is
synchronized()used here? - What is generally need
synchronized(), where to use it?
I read about multithreading, but did not really understand what and how. I would be happy to explain in simple words.