Java tutorial
/** * Copyright 2015 Google Inc. All Rights Reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package asia.covisoft.goom; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import asia.covisoft.goom.gcm.GcmPreferences; import asia.covisoft.goom.gcm.RegistrationIntentService; import asia.covisoft.goom.utils.NetworkClient; public class IntroActivity extends AppCompatActivity { private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private BroadcastReceiver mRegistrationBroadcastReceiver; private boolean intro; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intro); mContext = this; intro = false; mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // SharedPreferences sharedPreferences = // PreferenceManager.getDefaultSharedPreferences(context); // boolean sentToken = sharedPreferences // .getBoolean(GcmPreferences.SENT_TOKEN_TO_SERVER, false); // if (sentToken) { // mInformationTextView.setText(getString(R.string.gcm_send_message)); // } else { // mInformationTextView.setText(getString(R.string.token_error_message)); // } startMain(); } }; checkConnection(); } private ProgressDialog progressDialog; private void checkConnection() { NetworkClient.checkInternetConnection(5, new NetworkClient.OnConnectedListener() { @Override public void onPreConnect() { progressDialog = ProgressDialog.show(mContext, null, getString(R.string.dialog_connecting)); } @Override public void onConnected() { progressDialog.dismiss(); registerGCM(); } @Override public void onFail() { progressDialog.dismiss(); new AlertDialog.Builder(mContext).setCancelable(false) .setTitle(getString(R.string.dialog_connectionfailed_title)) .setMessage(getString(R.string.dialog_connectionfailed_message)).setPositiveButton( getString(R.string.lowcase_tryagain), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { checkConnection(); } }) .show(); } }); } private void registerGCM() { if (checkPlayServices()) { // Start IntentService to register this application with GCM. Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } new Handler().postDelayed(new Runnable() { @Override public void run() { startMain(); } }, 3000); // startMain(); } private void startMain() { if (intro) { startActivity(new Intent(this, MainActivity.class)); this.finish(); } else { intro = true; } } @Override protected void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(GcmPreferences.REGISTRATION_COMPLETE)); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); super.onPause(); } /** * Check the device to make sure it has the Google Play Services APK. If * it doesn't, display a dialog that allows users to download the APK from * the Google Play Store or enable it in the device's system settings. */ private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Log.i("sdb", "This device is not supported."); finish(); } return false; } return true; } }