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.umeng.common.ui.fragments.FollowedTopicFragment.java

@Override
public void onStart() {
    super.onStart();
    mBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from w  w w.j  a  v a  2s.co m*/
        public void onReceive(Context context, Intent intent) {
            mPresenter.loadDataFromServer();
        }
    };
    IntentFilter intentFilter = new IntentFilter(Constants.TOPIC_ACTION);
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mBroadcastReceiver, intentFilter);
}

From source file:com.renjunzheng.vendingmachine.SplashScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*View decorView = getWindow().getDecorView();
    // Hide the status bar.// w ww.  j  a v  a 2 s .c om
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);*/
    setContentView(R.layout.activity_splash_screen);

    //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);
            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);

    // 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:com.radioyps.gcm_test.MainActivity.java

@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/*  w ww  .j  a  v a  2s.  c o m*/
        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);
    }

}

From source file:edu.scu.userinterface.RegistrationActivity.java

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

    emailid = (EditText) findViewById(R.id.emailid);
    password = (EditText) findViewById(R.id.password);
    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override/*from   w ww .  j  a  v  a2s  .  co m*/
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(AppPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                Toast.makeText(RegistrationActivity.this, getString(R.string.gcm_send_message),
                        Toast.LENGTH_LONG);
                Intent loginIntent = new Intent(RegistrationActivity.this, LoginActivity.class);
                startActivity(loginIntent);
            } else {
                Toast.makeText(RegistrationActivity.this, getString(R.string.token_error_message),
                        Toast.LENGTH_LONG);
            }
        }
    };

    registerBtn = (Button) findViewById(R.id.registerBtn);
    registerBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validateUserInput() && checkPlayServices()) {
                // Start IntentService to register this application with GCM.
                Intent intent = new Intent(RegistrationActivity.this, RegistrationIntentService.class);
                intent.putExtra("email", emailid.getText().toString().trim());
                intent.putExtra("password", password.getText().toString().trim());
                mRegistrationProgressBar.setVisibility(ProgressBar.VISIBLE);
                startService(intent);
            }
        }
    });

    // Registering BroadcastReceiver
    registerReceiver();
}

From source file:com.artur.softwareproject.SensorDataListAdapter.java

SensorDataListAdapter(Activity context, String[] datenTypen, String[] datenEinheit) {
    super(context, R.layout.sensordata_list_pattern, datenTypen);

    this.dataTypes = datenTypen;
    this.dataUnits = datenEinheit;

    temperature = 0;// ww  w. j  a va  2  s.c o m
    humidity = 0;
    xPos = 0;
    yPos = 0;
    zPos = 0;

    BroadcastReceiver temperatureReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            temperature = (double) intent.getExtras().get("ambientTemperature");

        }
    };

    BroadcastReceiver gpsReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            double[] gps = (double[]) intent.getExtras().get("gpsDistance");

            if (gps == null) {
                Log.d(TAG, "Error while receiving gpsDistance.");
                return;
            }

            xPos = gps[0];
            yPos = gps[1];
        }
    };

    BroadcastReceiver baroReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            zPos = (double) intent.getExtras().get("hDiff");
        }
    };

    BroadcastReceiver humidityReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            humidity = (double) intent.getExtras().get("humidity");
        }
    };

    BroadcastReceiver opticalReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            illuminance = (double) intent.getExtras().get("light");
        }
    };

    LocalBroadcastManager.getInstance(context).registerReceiver(temperatureReceive,
            new IntentFilter("temperatureFilter"));
    LocalBroadcastManager.getInstance(context).registerReceiver(humidityReceive,
            new IntentFilter("humidityFilter"));
    LocalBroadcastManager.getInstance(context).registerReceiver(opticalReceive,
            new IntentFilter("lightFilter"));
    LocalBroadcastManager.getInstance(context).registerReceiver(gpsReceive, new IntentFilter("gpsDistFilter"));
    LocalBroadcastManager.getInstance(context).registerReceiver(baroReceive, new IntentFilter("hDiffFilter"));
}

From source file:com.github.tdudziak.gps_lock_lock.ControlActivity.java

/** Called when the activity is first created. */
@Override/*from www .j  a  v a 2  s.  c  o m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTextStatus = (TextView) findViewById(R.id.textStatus);
    mProgressStatus = (ProgressBar) findViewById(R.id.progressStatus);

    // setup the menu ListView
    mListMenu = (ListView) findViewById(R.id.listMenu);
    mMenuAdapter = new MenuAdapter(this, R.menu.menu);
    mListMenu.setAdapter(mMenuAdapter);
    mListMenu.setOnItemClickListener(this);

    mUiUpdateBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int left = intent.getIntExtra(LockService.EXTRA_TIME_LEFT, -1);
            assert left != -1;

            setStatus(left);

            if (left <= 0) {
                // This is the last message; no time left. Shutdown.
                finish();
            }
        }
    };
}

From source file:com.google.example.gcmnetworkmanagerquickstart.MainActivity.java

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

    // [START get_gcm_network_manager]
    mGcmNetworkManager = GcmNetworkManager.getInstance(this);
    // [END get_gcm_network_manager]

    // BroadcastReceiver to get information from MyTaskService about task completion.
    mReceiver = new BroadcastReceiver() {
        @Override/*from w ww  .ja  v  a  2 s  . co m*/
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(MyTaskService.ACTION_DONE)) {
                String tag = intent.getStringExtra(MyTaskService.EXTRA_TAG);
                int result = intent.getIntExtra(MyTaskService.EXTRA_RESULT, -1);

                String msg = String.format("DONE: %s (%d)", tag, result);
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
    };

    findViewById(R.id.button_start_wifi_task).setOnClickListener(this);
    findViewById(R.id.button_start_charging_task).setOnClickListener(this);
    findViewById(R.id.button_turn_on_wifi).setOnClickListener(this);
    findViewById(R.id.button_start_periodic_task).setOnClickListener(this);
    findViewById(R.id.button_stop_periodic_task).setOnClickListener(this);

    // Check that Google Play Services is available, since we need it to use GcmNetworkManager
    // but the API does not use GoogleApiClient, which would normally perform the check
    // automatically.
    checkPlayServicesAvailable();
}

From source file:com.callba.phone.ui.NewFriendsMsgActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = (ListView) findViewById(R.id.list);
    dao = new InviteMessgeDao(this);
    msgs = dao.getMessagesList();/*  w  w  w.  j a v  a  2  s . com*/
    //adapter
    adapter = new NewFriendsMsgAdapter(this, 1, msgs);
    listView.setAdapter(adapter);
    dao.saveUnreadMessageCount(0);
    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            dao.saveUnreadMessageCount(0);
            msgs = dao.getMessagesList();
            adapter.notifyDataSetChanged();
            adapter = new NewFriendsMsgAdapter(NewFriendsMsgActivity.this, 1, msgs);
            listView.setAdapter(adapter);
        }
    };
    broadcastReceiver1 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            dao.saveUnreadMessageCount(0);
            msgs = dao.getMessagesList();
            adapter.notifyDataSetChanged();
            adapter = new NewFriendsMsgAdapter(NewFriendsMsgActivity.this, 1, msgs);
            listView.setAdapter(adapter);
        }
    };
    localBroadcastManager.registerReceiver(broadcastReceiver, new IntentFilter(Constant.ACTION_GROUP_NOTIFY));
    localBroadcastManager.registerReceiver(broadcastReceiver1,
            new IntentFilter(Constant.ACTION_CONTACT_CHANAGED));
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(0526);
}

From source file:com.example.rclark.gcmhello.MainActivity.java

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

    mReceiveMessage = (TextView) findViewById(R.id.receive_msg);
    mSendMessage = (EditText) findViewById(R.id.send_msg);

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);

    //Set up a receiver to wait for the token to be returned (at which point we can get GCM messages)
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override//from  ww  w.  j  a va  2  s.c o m
        public void onReceive(Context context, Intent intent) {
            String param = intent.getStringExtra("param");

            if (QuickstartPreferences.REGISTRATION_COMPLETE.equals(param)) {
                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));
                }
            } else {
                updateMsg(param);
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    // Registering BroadcastReceiver
    registerReceiver();

    //set up button click handler
    Button sendButton = (Button) findViewById(R.id.send_button);

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendMessage();
        }
    });

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

}

From source file:com.Duo.music.player.Dialogs.ABRepeatDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    mContext = getActivity().getApplicationContext();
    mApp = (Common) mContext;//w  ww  .  j  a va  2 s  .  c  om

    receiver = new BroadcastReceiver() {

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

        }

    };

    sharedPreferences = getActivity().getSharedPreferences("com.jams.music.player", Context.MODE_PRIVATE);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_repeat_song_range_dialog, null);
    currentSongIndex = mApp.getService().getCurrentSongIndex();

    repeatSongATime = (TextView) view.findViewById(R.id.repeat_song_range_A_time);
    repeatSongBTime = (TextView) view.findViewById(R.id.repeat_song_range_B_time);

    currentSongDurationMillis = (int) mApp.getService().getCurrentMediaPlayer().getDuration();
    currentSongDurationSecs = (int) currentSongDurationMillis / 1000;

    //Remove the placeholder seekBar and replace it with the RangeSeekBar.
    seekBar = (SeekBar) view.findViewById(R.id.repeat_song_range_placeholder_seekbar);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) seekBar.getLayoutParams();
    viewGroup = (ViewGroup) seekBar.getParent();
    viewGroup.removeView(seekBar);

    rangeSeekBar = new RangeSeekBar<Integer>(0, currentSongDurationSecs, getActivity());
    rangeSeekBar.setLayoutParams(params);
    viewGroup.addView(rangeSeekBar);

    if (sharedPreferences.getInt(Common.REPEAT_MODE, Common.REPEAT_OFF) == Common.A_B_REPEAT) {
        repeatSongATime.setText(convertMillisToMinsSecs(mApp.getService().getRepeatSongRangePointA()));
        repeatSongBTime.setText(convertMillisToMinsSecs(mApp.getService().getRepeatSongRangePointB()));

        rangeSeekBar.setSelectedMinValue(mApp.getService().getRepeatSongRangePointA());
        rangeSeekBar.setSelectedMaxValue(mApp.getService().getRepeatSongRangePointB());

        repeatPointA = mApp.getService().getRepeatSongRangePointA();
        repeatPointB = mApp.getService().getRepeatSongRangePointB();
    } else {
        repeatSongATime.setText("0:00");
        repeatSongBTime.setText(convertMillisToMinsSecs(currentSongDurationMillis));
        repeatPointA = 0;
        repeatPointB = currentSongDurationMillis;
    }

    //Set the dialog title.
    builder.setTitle(R.string.a_b_repeat);
    builder.setView(view);
    builder.setNegativeButton(R.string.cancel, new OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub

        }

    });

    builder.setPositiveButton(R.string.repeat, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if ((currentSongDurationSecs - repeatPointB) < mApp.getCrossfadeDuration()) {
                //Remove the crossfade handler.
                mApp.getService().getHandler().removeCallbacks(mApp.getService().startCrossFadeRunnable);
                mApp.getService().getHandler().removeCallbacks(mApp.getService().crossFadeRunnable);
            }

            mApp.broadcastUpdateUICommand(new String[] { Common.UPDATE_PLAYBACK_CONTROLS },
                    new String[] { "" });
            mApp.getService().setRepeatSongRange(repeatPointA, repeatPointB);
            mApp.getService().setRepeatMode(Common.A_B_REPEAT);

        }

    });

    return builder.create();
}