Java tutorial
package com.marstemp.app.activities; import android.app.AlertDialog.Builder; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; import android.net.Uri; import android.os.Build; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.text.TextUtils; import com.chopping.activities.BaseActivity; import com.chopping.application.BasicPrefs; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.marstemp.R; import com.marstemp.app.fragments.AboutDialogFragment.EulaConfirmationDialog; import com.marstemp.utils.Prefs; /** * The basic {@link android.app.Activity} of application. * * @author Xinyue Zhao */ public class MarsTempActivity extends BaseActivity { //------------------------------------------------ //Subscribes, event-handlers //------------------------------------------------ /** * Handler for {@link}. * * @param e * Event {@link}. */ public void onEvent(Object e) { } //------------------------------------------------ /** * Calculate height of actionbar. * * @return Height of system defined actionbar. */ public static int calcActionBarHeight(Context conte) { int[] abSzAttr; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { abSzAttr = new int[] { android.R.attr.actionBarSize }; } else { abSzAttr = new int[] { R.attr.actionBarSize }; } TypedArray a = conte.obtainStyledAttributes(abSzAttr); return a.getDimensionPixelSize(0, -1); } /** * Show {@link android.support.v4.app.DialogFragment}. * * @param _dlgFrg * An instance of {@link android.support.v4.app.DialogFragment}. * @param _tagName * Tag name for dialog, default is "dlg". To grantee that only one instance of {@link * android.support.v4.app.DialogFragment} can been seen. */ protected void showDialogFragment(DialogFragment _dlgFrg, String _tagName) { try { if (_dlgFrg != null) { DialogFragment dialogFragment = _dlgFrg; FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); // Ensure that there's only one dialog to the user. Fragment prev = getSupportFragmentManager().findFragmentByTag("dlg"); if (prev != null) { ft.remove(prev); } try { if (TextUtils.isEmpty(_tagName)) { dialogFragment.show(ft, "dlg"); } else { dialogFragment.show(ft, _tagName); } } catch (Exception _e) { } } } catch (Exception _e) { } } @Override protected BasicPrefs getPrefs() { return Prefs.getInstance(); } @Override protected void onResume() { super.onResume(); checkPlayService(); } /** * To confirm whether the validation of the Play-service of Google Inc. */ private void checkPlayService() { final int isFound = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (isFound == ConnectionResult.SUCCESS) {//Ignore update. //The "End User License Agreement" must be confirmed before you use this application. if (!Prefs.getInstance().isEULAOnceConfirmed()) { showDialogFragment(new EulaConfirmationDialog(), null); } } else { new Builder(this).setTitle(R.string.application_name).setMessage(R.string.lbl_play_service) .setCancelable(false) .setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.dismiss(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(getString(R.string.play_service_url))); try { startActivity(intent); } catch (ActivityNotFoundException e0) { intent.setData(Uri.parse(getString(R.string.play_service_web))); try { startActivity(intent); } catch (Exception e1) { //Ignore now. } } finally { finish(); } } }).create().show(); } } }