Java tutorial
package com.radioyps.gcm_test; /** * Copyright 2015 Google Inc. All Rights Reserved. * * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. */ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.journeyapps.barcodescanner.BarcodeEncoder; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private BroadcastReceiver mRegistrationBroadcastReceiver; private TextView mStatusTextView; private ImageView imageView; private boolean isReceiverRegistered = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); mStatusTextView = (TextView) findViewById(R.id.status_info); imageView = (ImageView) findViewById(R.id.QrImage); mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceiver()>> ...."); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); boolean sentToken = sharedPreferences.getBoolean(CommonConstants.PREF_IS_LOCAL_TOKEN_RECEVIED, false); if (sentToken) { mStatusTextView.setText(getString(R.string.gcm_send_message)); String token = sharedPreferences.getString(CommonConstants.PREF_LOCAL_TOKEN_SAVING_KEY, null); if (token != null) makeQRCode(token); } else { mStatusTextView.setText(getString(R.string.token_error_message)); } } }; // Registering BroadcastReceiver registerReceiver(); if (checkPlayServices()) { GCMGateWay.start("MainActivity start", this); } } @Override protected void onResume() { super.onResume(); registerReceiver(); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); isReceiverRegistered = false; super.onPause(); } private void registerReceiver() { if (!isReceiverRegistered) { IntentFilter msgIntentFilter = new IntentFilter(CommonConstants.LOCAL_BROADCAST_ACTION); msgIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, msgIntentFilter); isReceiverRegistered = true; } } /** * 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(TAG, "This device is not supported."); finish(); } return false; } return true; } @Override protected void onDestroy() { super.onDestroy(); } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); String token = null; //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { Log.d(TAG, "setting pressed "); startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } private void makeQRCode(String token) { Log.d(TAG, "displayTokernOnScreen()>> <" + token + ">"); MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); try { BitMatrix bitMatrix = multiFormatWriter.encode(token, BarcodeFormat.QR_CODE, 200, 200); BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix); imageView.setImageBitmap(bitmap); ; } catch (WriterException e) { e.printStackTrace(); } } }