Android Open Source - androidto-alerts Main Activity






From Project

Back to project page androidto-alerts.

License

The source code is released under:

# License This license applies to all parts of this project that are not externally maintained libraries. The externally maintained libraries used by this project are: - Android C2DM Library, locat...

If you think the Android project androidto-alerts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rahulagarwal.android.androidtoalerts;
/*from   www. j  av  a  2 s .c  o  m*/
import android.app.Activity;
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.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.c2dm.C2DMessaging;

public class MainActivity extends Activity {
  
  
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        SharedPreferences settings = getSharedPreferences(Constants.PREF_NAME, 0);
        String pushRegistrationID = settings.getString("pushRegistrationID", null);
        if (pushRegistrationID != null) {
          // Registered with C2DM
          String lastAlert = settings.getString(Constants.LAST_ALERT_MESSAGE, "null");
          setContentView(R.layout.connected);
          if (lastAlert != null) {
            TextView view = (TextView)findViewById(R.id.last_alert);
            view.setText(lastAlert);
          }
        } else {
          //Not Registered with C2DM
          setContentView(R.layout.notconnected);
        } 
        registerReceiver(mUpdateUIErrorReceiver, new IntentFilter(Constants.UPDATE_UI_ACTION_ERROR));
        registerReceiver(mUpdateUIReceiver, new IntentFilter(Constants.UPDATE_UI_ACTION));
    }
  
  @Override
    public void onDestroy() {
        unregisterReceiver(mUpdateUIReceiver);
        unregisterReceiver(mUpdateUIErrorReceiver);
        super.onDestroy();
    }

    
    public void connectToPush(View view) {
      Log.d(Constants.LOG_TAG, "Connecting To Push");
      C2DMessaging.register(getApplicationContext(), Constants.SENDER_ACCOUNT_EMAIL);
      setBusyIndicator(true);
      
    }
    
    public void disconnectToPush(View view) {
      Log.d(Constants.LOG_TAG, "Disconnecting To Push");
      C2DMessaging.unregister(getApplicationContext());
      setBusyIndicator(true);
    }
    
    public void setBusyIndicator(boolean mode) {
    ProgressBar bar = (ProgressBar)findViewById(R.id.progress_main);
    if (bar != null) {
      bar.setVisibility(mode?View.VISIBLE:View.INVISIBLE);
    }
  }
    
    private final BroadcastReceiver mUpdateUIErrorReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setBusyIndicator(false);
            findViewById(R.id.connection_error).setVisibility(View.VISIBLE);
        }
    };
    
    private final BroadcastReceiver mUpdateUIReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
          setBusyIndicator(false);
            int status = intent.getIntExtra(Constants.STATUS_EXTRA, Constants.ERROR_STATUS);
          if (status == Constants.REGISTERED_STATUS) {
            setContentView(R.layout.connected);
          } else if (status == Constants.UNREGISTERED_STATUS) {
            setContentView(R.layout.notconnected);
          } else if (status == Constants.ERROR_STATUS || status == Constants.AUTH_ERROR_STATUS) {
            findViewById(R.id.connection_error).setVisibility(View.VISIBLE);
          }
        }
    };
    
    
}




Java Source Code List

com.google.android.c2dm.C2DMBaseReceiver.java
com.google.android.c2dm.C2DMBroadcastReceiver.java
com.google.android.c2dm.C2DMessaging.java
com.rahulagarwal.android.androidtoalerts.C2DMReceiver.java
com.rahulagarwal.android.androidtoalerts.Constants.java
com.rahulagarwal.android.androidtoalerts.DeviceRegistrar.java
com.rahulagarwal.android.androidtoalerts.MainActivity.java