Example usage for android.support.v4.content LocalBroadcastManager getInstance

List of usage examples for android.support.v4.content LocalBroadcastManager getInstance

Introduction

In this page you can find the example usage for android.support.v4.content LocalBroadcastManager getInstance.

Prototype

public static LocalBroadcastManager getInstance(Context context) 

Source Link

Usage

From source file:com.android.example.notificationlistener.NotificationListenerActivity.java

public void launch(View v) {
    Log.d(TAG, "clicked launch");
    Object tag = v.getTag();/*  w  ww.  j  a va2  s  .c o  m*/
    if (tag instanceof StatusBarNotification) {
        StatusBarNotification sbn = (StatusBarNotification) tag;
        Log.d(TAG, "  on " + sbn.getKey());
        LocalBroadcastManager.getInstance(this)
                .sendBroadcast(new Intent(Listener.ACTION_LAUNCH).putExtra(Listener.EXTRA_KEY, sbn.getKey()));
    }
}

From source file:com.android.camera.processing.ProcessingService.java

@Override
public void onDestroy() {
    Log.d(TAG, "Shutting down");
    // TODO: Cancel session in progress...

    // Unlock the power manager, i.e. let power management kick in if
    // needed.//  www.  j  a  va 2 s.co m
    if (mWakeLock.isHeld()) {
        mWakeLock.release();
    }
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceController);
    stopForeground(true);
}

From source file:ch.luethi.skylinestracker.MainActivity.java

public void startStopTracking(View view) {
    CheckBox cb = (CheckBox) view;/*from   w ww  .ja  va 2 s  .c  o  m*/
    if (cb.isChecked()) {
        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!provider.contains("gps")) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
        LocalBroadcastManager.getInstance(this).registerReceiver(onStatusChange, brFilter);
        startService(positionService);
        statusText.setText(R.string.on);
    } else {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(onStatusChange);
        stopService(positionService);
        statusText.setText(R.string.off);
    }
}

From source file:ca.appvelopers.mcgillmobile.util.thread.UserDownloader.java

/**
 * Handles any exception while downloading the user info
 *
 * @param e       Exception instance/*from w w w .  ja  v a2s.  c om*/
 * @param section Section that the exception happened in
 */
private void handleException(IOException e, String section) {
    if (e instanceof MinervaException) {
        LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(Constants.BROADCAST_MINERVA));
    } else if (!(e instanceof SSLException)) {
        // Don't log SSLExceptions
        Timber.e(new Exception("Exception: " + section, e), "");
    }
    exception = e;
}

From source file:ca.hoogit.garagepi.Utils.Helpers.java

/**
 * Helper method to broadcast the outcome of the service
 *
 * @param action     Calling action//  w  w w  .j a  va 2 s  . c o m
 * @param wasSuccess Whether or not action was successful
 * @param message    Outcome message
 */
public static void broadcast(Context context, String filter, String action, boolean wasSuccess,
        String message) {
    Intent broadcast = new Intent(filter);
    broadcast.putExtra(Consts.KEY_BROADCAST_ACTION, action);
    broadcast.putExtra(Consts.KEY_BROADCAST_SUCCESS, wasSuccess);
    broadcast.putExtra(Consts.KEY_BROADCAST_MESSAGE, message);
    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
    Log.d(TAG, "broadcast: Message: " + wasSuccess + " - " + message);
}

From source file:ch.luethi.skylinestracker.PositionService.java

private void sendPositionStatus() {
    LocalBroadcastManager.getInstance(this).sendBroadcast(intentPosStatus);
}

From source file:com.android.server.telecom.testapps.CallNotificationReceiver.java

public static void sendChangeAngleIntent(Context context, Intent intent) {
    final Intent intentChangeAngle = new Intent(TestCallActivity.ACTION_CHANGE_ANGLE);
    intentChangeAngle.setData(intent.getData());
    LocalBroadcastManager.getInstance(context).sendBroadcast(intentChangeAngle);
}

From source file:cc.softwarefactory.lokki.android.fragments.MapViewFragment.java

@Override
public void onResume() { // onResume is called after onActivityCreated, when the fragment is loaded 100%

    Log.e(TAG, "onResume");
    super.onResume();
    if (map == null) {
        Log.e(TAG, "Map null. creating it.");
        setUpMap();/*from  w  w  w.j a  v  a2 s.c o  m*/
        setupAddPlacesOverlay();
    } else {
        Log.e(TAG, "Map already exists. Nothing to do.");
    }

    LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
            new IntentFilter("LOCATION-UPDATE"));
    LocalBroadcastManager.getInstance(context).registerReceiver(placesUpdateReceiver,
            new IntentFilter("PLACES-UPDATE"));

    checkLocationServiceStatus();

    new UpdateMap().execute(MapUserTypes.All);
    cancelAsyncTasks = false;
    if (MainApplication.places != null) {
        updatePlaces();
    }
}

From source file:bolts.AppLinkTest.java

public void testGeneralMeasurementEventsBroadcast() throws Exception {
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    i.putExtra("foo", "bar");
    ArrayList<String> arr = new ArrayList<>();
    arr.add("foo2");
    arr.add("bar2");
    i.putExtra("foobar", arr);
    Map<String, String> other = new HashMap<>();
    other.put("yetAnotherFoo", "yetAnotherBar");

    final CountDownLatch lock = new CountDownLatch(1);
    final String[] receivedStrings = new String[5];
    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getInstrumentation().getTargetContext());
    manager.registerReceiver(new BroadcastReceiver() {
        @Override/*w  w  w.  jav a  2 s.co m*/
        public void onReceive(Context context, Intent intent) {
            String eventName = intent.getStringExtra("event_name");
            Bundle eventArgs = intent.getBundleExtra("event_args");
            receivedStrings[0] = eventName;
            receivedStrings[1] = eventArgs.getString("foo");
            receivedStrings[2] = eventArgs.getString("foobar");
            receivedStrings[3] = eventArgs.getString("yetAnotherFoo");
            receivedStrings[4] = eventArgs.getString("intentData");
            lock.countDown();
        }
    }, new IntentFilter("com.parse.bolts.measurement_event"));

    MeasurementEvent.sendBroadcastEvent(getInstrumentation().getTargetContext(), "myEventName", i, other);
    lock.await(2000, TimeUnit.MILLISECONDS);

    assertEquals("myEventName", receivedStrings[0]);
    assertEquals("bar", receivedStrings[1]);
    assertEquals((new JSONArray(arr)).toString(), receivedStrings[2]);
    assertEquals("yetAnotherBar", receivedStrings[3]);
    assertEquals("http://www.example.com", receivedStrings[4]);
}

From source file:com.android.prachat.gcm.MyGcmPushReceiver.java

/**
 * Processing chat room push message/*  w  w w .jav a2  s .  c  o  m*/
 * this message will be broadcasts to all the activities registered
 * */
private void processChatRoomPush(String title, boolean isBackground, String data) {
    if (!isBackground) {

        try {
            JSONObject datObj = new JSONObject(data);

            String chatRoomId = datObj.getString("chat_room_id");

            JSONObject mObj = datObj.getJSONObject("message");
            Message message = new Message();
            message.setMessage(mObj.getString("message"));
            message.setId(mObj.getString("message_id"));
            message.setCreatedAt(mObj.getString("created_at"));

            JSONObject uObj = datObj.getJSONObject("user");

            // skip the message if the message belongs to same user as
            // the user would be having the same message when he was sending
            // but it might differs in your scenario
            if (uObj.getString("user_id")
                    .equals(MyApplication.getInstance().getPrefManager().getUser().getId())) {
                Log.e(TAG, "Skipping the push message as it belongs to same user");
                return;
            }

            User user = new User();
            user.setId(uObj.getString("user_id"));
            user.setEmail(uObj.getString("email"));
            user.setName(uObj.getString("name"));
            message.setUser(user);

            // verifying whether the app is in background or foreground
            if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {

                // app is in foreground, broadcast the push message
                Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
                pushNotification.putExtra("type", Config.PUSH_TYPE_CHATROOM);
                pushNotification.putExtra("message", message);
                pushNotification.putExtra("chat_room_id", chatRoomId);
                LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

                // play notification sound
                NotificationUtils notificationUtils = new NotificationUtils();
                notificationUtils.playNotificationSound();
            } else {

                // app is in background. show the message in notification try
                Intent resultIntent = new Intent(getApplicationContext(), ChatRoomActivity.class);
                resultIntent.putExtra("chat_room_id", chatRoomId);
                showNotificationMessage(getApplicationContext(), title,
                        user.getName() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent);
            }

        } catch (JSONException e) {
            Log.e(TAG, "json parsing error: " + e.getMessage());
            Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG)
                    .show();
        }

    } else {
        // the push notification is silent, may be other operations needed
        // like inserting it in to SQLite
    }
}