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:edu.cmu.plugins.PhoneListener.java

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * //from w  w w. j  a  v  a  2s.  co m
 * @param ctx The context of the main Activity.
 */
public void setContext(PhonegapActivity ctx) {
    super.setContext(ctx);
    this.phoneListenerCallbackId = null;

    // We need to listen to connectivity events to update navigator.connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");

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

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null) {
                    return;
                }
                String state = "";
                if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                    state = "SMS_RECEIVED";
                    Log.i(LOG_TAG, state);
                }
                if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    // State has changed
                    String phoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : null;

                    // See if the new state is 'ringing', 'off hook' or 'idle'
                    if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        // phone is ringing, awaiting either answering or canceling
                        state = "RINGING";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        // actually talking on the phone... either making a call or having answered one
                        state = "OFFHOOK";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        // idle means back to no calls in or out. default state.
                        state = "IDLE";
                        Log.i(LOG_TAG, state);
                    } else {
                        state = TYPE_NONE;
                        Log.i(LOG_TAG, state);
                    }
                }
                updatePhoneState(state, true);
            }
        };
        // register the receiver... this is so it doesn't have to be added to AndroidManifest.xml
        ctx.registerReceiver(this.receiver, intentFilter);
    }
}

From source file:com.example.plugin.PhoneListener.java

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    this.phoneListenerCallbackId = null;
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override//from   w  w  w . j  ava 2 s . c om
            public void onReceive(Context context, Intent intent) {
                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    // State has changed
                    String phoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : null;
                    String state;
                    // See if the new state is 'ringing', 'off hook' or 'idle'
                    if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        // phone is ringing, awaiting either answering or canceling
                        state = "RINGING";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        // actually talking on the phone... either making a call or having answered one
                        state = "OFFHOOK";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        // idle means back to no calls in or out. default state.
                        state = "IDLE";
                        Log.i(LOG_TAG, state);
                    } else {
                        state = TYPE_NONE;
                        Log.i(LOG_TAG, state);
                    }
                    updatePhoneState(state, true);
                }
            }
        };
        // register the receiver... this is so it doesn't have to be added to AndroidManifest.xml
        cordova.getActivity().registerReceiver(this.receiver, intentFilter);
        this.registered = true;
    }

}

From source file:com.telldus.live.mobile.MainActivity.java

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

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from  ww w .  j  av a  2 s.  com*/
        public void onReceive(Context context, Intent intent) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                Log.d("JAVACODE", "gcm_send_message");
            } else {
                Log.d("JAVACODE", "token_error_message");
            }
        }
    };
}

From source file:com.quandrum.phonebridge.GcmActivity.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//  w w  w  .j a  v  a  2 s.c o 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);
            if (sentToken) {
                mInformationTextView.setText("sending");
            } else {
                mInformationTextView.setText("error");
            }
            finish();
        }
    };
    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.android.gcm.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   w  w  w.  jav  a2s .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));
                mInformationTextView.setText(sharedPreferences.getString("REGID", "nope"));
            } 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:com.ripperdesignandmultimedia.phoneblockplugin.PhoneBlockerPlugin.java

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * /*from w w  w  . j a  v a  2 s  . co m*/
 * @param ctx The context of the main Activity.
 */
public void setContext(CordovaInterface ctx) {
    super.setContext(ctx);
    this.phoneBlockerCallbackId = null;

    // We need to listen to connectivity events to update navigator.connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    // State has changed
                    String phoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : null;
                    String state;
                    // See if the new state is 'ringing', 'off hook' or 'idle'
                    if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        // phone is ringing, awaiting either answering or canceling
                        state = "RINGING";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        // actually talking on the phone... either making a call or having answered one
                        state = "OFFHOOK";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        // idle means back to no calls in or out. default state.
                        state = "IDLE";
                        Log.i(LOG_TAG, state);
                    } else {
                        state = TYPE_NONE;
                        Log.i(LOG_TAG, state);
                    }
                    updatePhoneState(state, true);
                }
            }
        };
        // register the receiver... this is so it doesn't have to be added to AndroidManifest.xml
        cordova.getActivity().registerReceiver(this.receiver, intentFilter);
    }
}

From source file:com.android.bleserver.AdvertiserFragment.java

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

    advertisingFailureReceiver = new BroadcastReceiver() {

        /**//w ww .j  a  v a 2s.  c om
         * Receives Advertising error codes from {@code AdvertiserService} and displays error messages
         * to the user. Sets the advertising toggle to 'false.'
         */
        @Override
        public void onReceive(Context context, Intent intent) {

            int errorCode = intent.getIntExtra(AdvertiserService.ADVERTISING_FAILED_EXTRA_CODE, -1);

            mSwitch.setChecked(false);

            String errorMessage = getString(R.string.start_error_prefix);
            switch (errorCode) {
            case AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED:
                errorMessage += " " + getString(R.string.start_error_already_started);
                break;
            case AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE:
                errorMessage += " " + getString(R.string.start_error_too_large);
                break;
            case AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED:
                errorMessage += " " + getString(R.string.start_error_unsupported);
                break;
            case AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR:
                errorMessage += " " + getString(R.string.start_error_internal);
                break;
            case AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:
                errorMessage += " " + getString(R.string.start_error_too_many);
                break;
            case AdvertiserService.ADVERTISING_TIMED_OUT:
                errorMessage = " " + getString(R.string.advertising_timedout);
                break;
            default:
                errorMessage += " " + getString(R.string.start_error_unknown);
            }

            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
        }
    };
}

From source file:com.example.shuvamghosh.piano.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  www  .  ja  v a 2s  . 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("Sent Token");
                Intent toPlayActivity = new Intent(MainActivity.this, PlayActivity.class);
                startActivity(toPlayActivity);
                finish();
            } else {
                mInformationTextView.setText("Error Token");
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    // Registering BroadcastReceiver
    registerReceiver();

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

From source file:asia.covisoft.goom.IntroActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intro);
    mContext = this;

    intro = false;//from w  w w. j  a  v  a 2s.c  o m

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //                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));
            //                } else {
            //                    mInformationTextView.setText(getString(R.string.token_error_message));
            //                }

            startMain();
        }
    };

    checkConnection();
}

From source file:com.example.user.rideshareapp1.RegisteringToken.java

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

    // mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override//from  w  ww  .  j a  va 2s  .c o  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);
            if (sentToken) {
                mInformationTextView.setText("It worked!");
            } else {
                mInformationTextView.setText("It didn't!");
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    // Registering BroadcastReceiver
    registerReceiver();

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