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:autobahn.demo.com.autobahndemo.EchoClientActivity.java

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);

}

From source file:com.air.mobilebrowser.ActivityWatchService.java

/**
 * Hide any application that is in the foreground and is not
 * in the white-list, and collapse the status bar.
 *//*from www .  j a  v  a  2s .  co m*/
private void hideApplications() {
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);

    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;

        String packageName = topActivity.getPackageName();

        if (!packageName.equals(getPackageName()) && !mApplicationWhitelist.contains(packageName)) {
            //We hide the activity, by bringing the BrowserActivity back in front.
            Intent intent = new Intent(this, BrowserActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

            //Visual indicator that we are hiding an app.
            Toast.makeText(this, "Blocked " + packageName, Toast.LENGTH_SHORT).show();
        }
    }

    try {
        final Object service = getSystemService("statusbar");
        Class<?> statusbarManager = Class.forName("android.app.StatusBarManager");

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {
            final Method collapse = statusbarManager.getMethod("collapse");
            collapse.setAccessible(true);
            collapse.invoke(service);
        } else {
            final Method collapse = statusbarManager.getMethod("collapsePanels");
            collapse.setAccessible(true);
            collapse.invoke(service);
        }
    } catch (Exception ex) {
        Log.e(TAG, "Failed to collapse status bar");
    }

    List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();

    for (RunningAppProcessInfo info : appList) {
        am.killBackgroundProcesses(info.processName);
    }

    appList = am.getRunningAppProcesses();

    for (RunningAppProcessInfo info : appList) {
        if (mAppBlacklist.contains(info.processName)) {
            Intent intent = new Intent(super.getResources().getString(R.string.intent_black_logtag));
            LocalBroadcastManager.getInstance(super.getApplicationContext()).sendBroadcast(intent);
        }
    }

}

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

/**
 * Processing user specific push message
 * It will be displayed with / without image in push notification tray
 * *///from w  w  w  .j  a va2 s .  c o  m
private void processUserMessage(String title, boolean isBackground, String data) {
    if (!isBackground) {

        try {
            JSONObject datObj = new JSONObject(data);

            String imageUrl = datObj.getString("image");

            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");
            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_USER);
                pushNotification.putExtra("message", message);
                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(), MainActivity.class);

                // check for push notification image attachment
                if (TextUtils.isEmpty(imageUrl)) {
                    showNotificationMessage(getApplicationContext(), title,
                            user.getName() + " : " + message.getMessage(), message.getCreatedAt(),
                            resultIntent);
                } else {
                    // push notification contains image
                    // show it with the image
                    showNotificationMessageWithBigImage(getApplicationContext(), title, message.getMessage(),
                            message.getCreatedAt(), resultIntent, imageUrl);
                }
            }
        } 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
    }
}

From source file:com.alphabetbloc.accessmrs.ui.user.BaseUserActivity.java

@Override
protected void onResume() {
    mPaused = false;/*from  w w  w.  j  a v a2s .c  om*/
    super.onResume();
    IntentFilter filter = new IntentFilter(SyncManager.SYNC_MESSAGE);
    LocalBroadcastManager.getInstance(this).registerReceiver(onSyncNotice, filter);
    mSyncObserverHandle = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE,
            this);

    if (RefreshDataService.isSyncActive)
        updateSyncProgress();
    else {
        if (mSyncActiveDialog != null) {
            mSyncActiveDialog.dismiss();
            mSyncActiveDialog = null;
        }
    }
}

From source file:com.anjalimacwan.fragment.NoteListFragment.java

@Override
public void onStart() {
    super.onStart();

    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);

    // Floating action button
    FloatingActionButton floatingActionButton = (FloatingActionButton) getActivity()
            .findViewById(R.id.button_floating_action);
    floatingActionButton.setImageResource(R.drawable.ic_action_new);
    if (getActivity().findViewById(R.id.layoutMain).getTag().equals("main-layout-large"))
        floatingActionButton.hide();/*from   ww w  .j a  va  2s.  com*/

    SharedPreferences prefMain = getActivity().getPreferences(Context.MODE_PRIVATE);

    if (getId() == R.id.noteViewEdit && prefMain.getLong("draft-name", 0) == 0) {
        floatingActionButton.show();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("filename", "new");

                Fragment fragment = new NoteEditFragment();
                fragment.setArguments(bundle);

                // Add NoteEditFragment
                getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteEditFragment")
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
            }
        });
    }
}

From source file:com.acrr.acdisplay.services.activemode.ActiveModeService.java

@Override
public void onCreate() {
    Context context = getContext();
    mSensors = buildAvailableSensorsList(context);
    super.onCreate();

    IntentFilter filter = new IntentFilter();
    filter.addAction(App.ACTION_INTERNAL_PING_SENSORS);
    LocalBroadcastManager.getInstance(context).registerReceiver(mLocalReceiver, filter);

    NotificationPresenter.getInstance().registerListener(this);
}

From source file:ca.rmen.android.scrumchatter.main.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    Theme.checkTheme(this);

    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    StrictModeUtil.enable();//from  w  w  w. j av  a  2  s.co  m

    // Set up the action bar.
    setSupportActionBar(mBinding.toolbarTabs.toolbar);
    ActionBar supportActionBar = getSupportActionBar();
    assert supportActionBar != null;
    supportActionBar.setDisplayHomeAsUpEnabled(true);
    supportActionBar.setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mBinding.drawerLayout, /* DrawerLayout object */
            R.string.drawer_open, /* "open drawer" description */
            R.string.drawer_close /* "close drawer" description */);

    // Explanation of setDrawerIndicatorEnabled and setHomeAsUpIndicator:
    // We want to only have a hamburger icon, always, without any animation.

    // If we don't call either of these methods, we use the default indicator,
    // which is the hamburger icon transitioning to a left-arrow icon, as the drawer is opened.

    // If we only call setDrawerIndicatorUpEnabled, we'll have the left arrow icon always.

    // If we only call setHomeAsUpIndicator (with a hamburger icon), we'll have a hamburger icon
    // but with a bug: If you open the drawer, rotate, and close it, you'll have the left arrow
    // again.

    // With the combination of both setDrawerIndicatorEnabled and setHomeAsUpIndicator, we
    // have a hamburger icon always.
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    mDrawerToggle.setHomeAsUpIndicator(new DrawerArrowDrawable(this));

    mMainPagerAdapter = new MainPagerAdapter(this, getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mBinding.pager.setAdapter(mMainPagerAdapter);
    mBinding.toolbarTabs.tabs.setupWithViewPager(mBinding.pager);

    // If our activity was opened by choosing a file from a mail attachment, file browser, or other program,
    // import the database from this file.
    Intent intent = getIntent();
    if (intent != null) {
        if (Intent.ACTION_VIEW.equals(intent.getAction()))
            importDB(intent.getData());
    }

    // Register various observers.
    mTeamsObserver = new TeamsObserver(this, mOnTeamsChangedListener);
    mOnTeamsChangedListener.onTeamsChanged();
    mTeamsObserver.register();
    mTeamNavigationMenu = new TeamNavigationMenu(this, mBinding.leftDrawer.getMenu());
    mTeamNavigationMenu.load();
    mBinding.leftDrawer.setNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    IntentFilter filter = new IntentFilter(ACTION_IMPORT_COMPLETE);
    filter.addAction(ACTION_EXPORT_COMPLETE);
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mBroadcastReceiver, filter);
}

From source file:ca.uwaterloo.magic.goodhikes.MapsActivity.java

@Override
protected void onStart() {
    super.onStart();
    mFilter = new IntentFilter(GPSLoggingService.locationUpdateCommand);
    mGPSUpdatesReceiver = new GPSUpdatesReceiver();
    mConnection = new GPSLoggingServiceConnection();
    if (selectedRoute == null)
        selectedRoute = database.getLatestRoute(userManager.getUser());
    //        clearMap();
    LocalBroadcastManager.getInstance(this).registerReceiver(mGPSUpdatesReceiver, mFilter);
    registerReceiver(logoutReceiver, new IntentFilter("logout"));
    startService(new Intent(this, GPSLoggingService.class));
    Log.d(LOG_TAG, "Thread: " + Thread.currentThread().getId() + "; MapActivity started");
}

From source file:com.android.screenspeak.tutorial.TutorialModule.java

/**
 * Registers a broadcast receiver with the parent tutorial's
 * {@link LocalBroadcastManager} using the specified filter.
 *
 * @param receiver The broadcast receiver to register.
 * @param filter The filter for which {@code Intent} objects to receive.
 *//*ww  w .  j av  a 2  s .c o m*/
void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
    final LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mParentTutorial);
    if (manager == null) {
        return;
    }

    manager.registerReceiver(receiver, filter);
}

From source file:ch.ethz.twimight.activities.LoginActivity.java

/**
 * Method used to register a login Receiver
 * /* w ww .  j  a v a 2  s  .com*/
 * @author pcarta
 */
private void registerLoginReceiver() {
    if (mLoginReceiver == null) {
        mLoginReceiver = new LoginReceiver();
    }
    IntentFilter intentFilter = new IntentFilter(LoginActivity.LOGIN_RESULT_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(mLoginReceiver, intentFilter);
}