Java tutorial
/** * Copyright [2015] [Codelyst (Prashant goyal)] * <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.se. */ package com.codelyst.pushnotification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.codelyst.ccm.RegistrationIntentService; import com.codelyst.ccm.utils.PlayServiceUtils; public class MainActivity extends AppCompatActivity { private ProgressBar mRegistrationProgressBar; private BroadcastReceiver mRegistrationBroadcastReceiver; private TextView mInformationTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mRegistrationProgressBar.setVisibility(ProgressBar.GONE); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); String sentToken = sharedPreferences.getString(RegistrationIntentService.GCM_TOKEN, null); if (sentToken != null && sentToken.length() > 0) { sendTokenToServer(sentToken); mInformationTextView.setText(sentToken); } else { mInformationTextView.setText(getString(R.string.token_error_message)); } } }; mInformationTextView = (TextView) findViewById(R.id.informationTextView); if (PlayServiceUtils.checkPlayServices(this)) { // Start IntentService to register this application with GCM. Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } private void sendTokenToServer(String sentToken) { Toast.makeText(getApplicationContext(), sentToken, Toast.LENGTH_LONG).show(); } @Override protected void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(RegistrationIntentService.REGISTRATION_COMPLETE)); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); super.onPause(); } }