Example usage for android.content BroadcastReceiver BroadcastReceiver

List of usage examples for android.content BroadcastReceiver BroadcastReceiver

Introduction

In this page you can find the example usage for android.content BroadcastReceiver BroadcastReceiver.

Prototype

public BroadcastReceiver() 

Source Link

Usage

From source file:com.fast.van.cloudmessaging.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gcm);

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from  w w  w .ja v a2  s . co  m*/
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);

            String token = sharedPreferences.getString(QuickstartPreferences.REGISTRATION_TOKEN, "");

            if (sentToken) {
                mInformationTextView.setText("gcm_send_message :" + token);
            } else {
                mInformationTextView.setText("token_error_message");
            }

            Bundle extras = getIntent().getExtras();
            if (extras != null) {

                String message = null;
                String orderId = null;

                message = extras.getString("message");
                orderId = extras.getString("orderId");

                mInformationTextView.setText(orderId + " MSG:" + message);
            }

            Log.i(TAG, "GCM Registration Token: " + token);
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

From source file:com.trackwheels.activities.SplashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    Utility.client = ((KinveyClient) getApplication()).getKinveyClient();
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override//from www  .j a v  a2 s  .c o  m
        public void onReceive(Context context, Intent intent) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(Constants.GCM.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                Toast.makeText(SplashActivity.this, getString(R.string.gcm_send_message), Toast.LENGTH_LONG)
                        .show();
            } else {
                Toast.makeText(SplashActivity.this, getString(R.string.token_error_message), Toast.LENGTH_LONG)
                        .show();
            }
        }
    };

    // Registering BroadcastReceiver
    registerReceiver();

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
    if (isReceiverRegistered) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

From source file:com.vxg.cnvrclient2.ApplicationController.java

public void initGCM(CloudClientActivity ma) {

    Log.i(TAG, "GCM: start");
    final CloudClientActivity ma1 = ma;
    // mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/* ww w .ja  v  a 2s  . co m*/
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "GCM: onRecieve");
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(VXGCCQuickstartPreferences.SENT_TOKEN_TO_SERVER,
                    false);
            if (sentToken) {
                Log.i(TAG, "GCM: " + ma1.getString(R.string.gcm_send_message));
            } else {
                Log.e(TAG, "GCM: " + ma1.getString(R.string.token_error_message));
            }
        }
    };
    // mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    // Registering BroadcastReceiver
    registerReceiver(ma);

    if (checkPlayServices(ma)) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(ma, com.vxg.cnvrclient2.gcm.RegistrationIntentService.class);
        Log.i(TAG, "GCM: startService");
        ma.startService(intent);
    }
}

From source file:com.nadmm.airports.wx.WxFragmentBase.java

protected void setupBroadcastFilter(String action) {
    mFilter = new IntentFilter();
    mFilter.addAction(action);// ww  w . ja va2  s.  c  o m

    mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(mFilter.getAction(0))) {
                handleBroadcast(intent);
            }
        }
    };
}

From source file:com.szanata.cordova.plugins.PhoneStateChangeListener.java

/**
 * creates a new BroadcastReceiver to listen whether the Telephony State changes
 *///w  ww . j  a v  a  2 s .c o  m
public void startPhoneListener() {

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) {

                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    String state = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : NONE;
                    String number = "";

                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    }
                    if (callbackContext != null) {
                        final JSONObject data = new JSONObject();
                        try {
                            data.put("state", state);
                            data.put("number", number);
                        } catch (final JSONException e) {
                        }
                        ;
                        callbackContext.success(data);
                    }
                }
            }
        };

        this.context.registerReceiver(this.receiver,
                new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));
    }
}

From source file:com.takeoffandroid.GCMDemo.activity.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from   ww  w  . jav a  2 s .com*/
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(GCMUtils.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                mInformationTextView.setText(getString(R.string.gcm_send_message));
            } else {
                mInformationTextView.setText(getString(R.string.token_error_message));
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, GCMRegistrationIntentService.class);
        startService(intent);
    } else {

        Toast.makeText(MainActivity.this,
                "Sorry! Play services are not installed in your application. Please download it to receive push notifications",
                Toast.LENGTH_SHORT).show();
    }
}

From source file:com.thyn.broadcast.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    Log.d(TAG, "I am in mainactivity");

    //mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from   w  w  w  .  j a  va  2s . com*/
        public void onReceive(Context context, Intent intent) {
            //mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            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));
                Log.d(TAG, "The token has already been sent to the server. Starting WelcomePageActivity.class");
                Intent i = new Intent(getBaseContext(), WelcomePageActivity.class);
                startActivity(i);
            } else {
                //mInformationTextView.setText(getString(R.string.token_error_message));
                Log.d(TAG,
                        "don't have the token in the SharedPreferences cache. Need to ask GCM server for a token that I can send to the app server");
            }
        }
    };
    //mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    // Registering BroadcastReceiver
    registerReceiver();

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.

        Log.d(TAG, "checkPlayServices() is successful. starting GcmRegistrationIntentService.class");
        Intent intent = new Intent(this, GcmRegistrationIntentService.class);
        startService(intent);
    }

}

From source file:bg.phpgcm2.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    registerReceiver(uiUpdated, new IntentFilter("RECEIVEDMES"));

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*w w w .  ja v  a  2  s  .c om*/
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                //mInformationTextView.setText(getString(R.string.gcm_send_message));
            } else {
                //mInformationTextView.setText(getString(R.string.token_error_message));
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

From source file:io.octo.bear.pago.PerformPurchaseSingle.java

private static BroadcastReceiver createPurchaseBroadcastReceiver(final String payload,
        final SingleSubscriber<? super Order> subscriber) {

    return new BroadcastReceiver() {
        @Override//from  ww w . j  av a  2 s  .co m
        public void onReceive(Context context, Intent data) {
            try {
                LocalBroadcastManager.getInstance(context).unregisterReceiver(this);

                final Bundle result = data.getExtras();

                final boolean success = result.getBoolean(BillingActivity.EXTRA_SUCCESS, false);
                if (!success) {
                    throw new BillingException(ResponseCode.ITEM_UNAVAILABLE);
                }
                final ResponseCode code = retrieveResponseCode(result);
                checkResponseAndThrowIfError(code);
                String originalJson = result.getString(RESPONSE_INAPP_PURCHASE_DATA);
                Log.e("JsonDate", originalJson + "");
                final Purchase purchase = GSON.fromJson(originalJson, Purchase.class);
                final Order order = new Order(purchase, result.getString(RESPONSE_INAPP_DATA_SIGNATURE),
                        originalJson);
                final boolean purchaseDataIsCorrect = TextUtils.equals(payload, purchase.developerPayload);
                Log.e("payload", purchase.developerPayload + " " + purchaseDataIsCorrect);
                if (purchaseDataIsCorrect) {
                    subscriber.onSuccess(order);
                } else {
                    throw new BillingException(ResponseCode.ERROR);
                }
            } catch (BillingException e) {
                subscriber.onError(e);
            }
        }
    };
}

From source file:com.kii.world.LoginActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login);/*from  w w w  .  j  a  v a2s.  co  m*/

    // link our variables to UI elements
    mUsernameField = (TextView) findViewById(R.id.username_field);
    mPasswordField = (TextView) findViewById(R.id.password_field);

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String errorMessage = intent.getStringExtra("ErrorMessage");
            Log.e("GCMTest", "Registration completed:" + errorMessage);
            if (errorMessage != null) {
                Toast.makeText(LoginActivity.this, "Error push registration:" + errorMessage, Toast.LENGTH_LONG)
                        .show();
            } else {
                Toast.makeText(LoginActivity.this, "Succeeded push registration", Toast.LENGTH_LONG).show();
            }
        }
    };

    //???
    if (!checkPlayServices()) {
        Toast.makeText(LoginActivity.this, "This application needs Google Play Services", Toast.LENGTH_LONG)
                .show();
        return;
    } else {
        Toast.makeText(LoginActivity.this, "Google Play Services OK", Toast.LENGTH_LONG).show();
    }

}