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:azzaoui.sociadee.MyGcmListenerService.java

/**
 * Called when message is received.//  w  w w  . j  a v a  2  s.c o  m
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    /* if (from.startsWith("/topics/")) {
    // message received from some topic.
     } else {
    // normal downstream message.
     }*/

    if (data.getString("action").equals("FETCH_PUBLIC_MESSAGES")) {
        Intent newMessage = new Intent(Parameters.PUBLIC_MESSAGE_RECEIVED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(newMessage);

    } else if (data.getString("action").equals("FETCH_PRIVATE_MESSAGES")) {
        Intent newMessage = new Intent(Parameters.PRIVATE_MESSAGE_RECEIVED);
        newMessage.putExtra("CONVID", data.getString("conv"));
        LocalBroadcastManager.getInstance(this).sendBroadcast(newMessage);
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    //sendNotification(message);
    // [END_EXCLUDE]
}

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

public void register() {
    String filter = getFilterName();
    if (filter != null && !filter.isEmpty()) {
        LocalBroadcastManager.getInstance(mContext).registerReceiver(this, new IntentFilter(filter));
    } else {// ww w.ja v a2 s.c om
        Log.e(TAG, "register: No filter string was provided");
    }
}

From source file:ca.farrelltonsolar.classic.MessageFragment.java

@Override
public void onStart() {
    super.onStart();
    if (!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(mReadingsReceiver,
                new IntentFilter(Constants.CA_FARRELLTONSOLAR_CLASSIC_READINGS));
        isReceiverRegistered = true;/*w w w.ja v a2 s . c  o m*/
    }
    Log.d(getClass().getName(), "onStart");
}

From source file:ca.zadrox.dota2esportticker.ui.TeamOverviewFragment.java

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

    if (getActivity() instanceof TeamDetailActivity) {
        mTeam = ((TeamDetailActivity) getActivity()).getTeam();
    }//from   www.  j  ava  2 s.c o  m

    if (mTeam == null) {
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mBroadcastReceiver,
                new IntentFilter(TeamDetailActivity.STATUS_LOAD_COMPLETE));
    } else {
        handleTeamInfo();
    }

    LogUtils.LOGD(TAG, "onResume");
    LogUtils.LOGD(TAG, "mTeam: " + (mTeam == null ? "null" : "set"));
}

From source file:ca.farrelltonsolar.classic.ReadingFramentBase.java

@Override
public void onStart() {
    super.onStart();
    if (!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(ReadingFramentBase.this.getActivity()).registerReceiver(
                mReadingsReceiver, new IntentFilter(Constants.CA_FARRELLTONSOLAR_CLASSIC_READINGS));
        isReceiverRegistered = true;//from   w ww .j a v a 2  s  . c o m
    }
    Log.d(getClass().getName(), "onStart");
}

From source file:cc.softwarefactory.lokki.android.utilities.ServerApi.java

public static void getDashboard(final Context context) {
    Log.e(TAG, "getDashboard");
    AQuery aq = new AQuery(context);

    String userId = PreferenceUtils.getString(context, PreferenceUtils.KEY_USER_ID);
    final String authorizationToken = PreferenceUtils.getString(context, PreferenceUtils.KEY_AUTH_TOKEN);
    String url = ApiUrl + "user/" + userId + "/dashboard";

    AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>() {
        @Override//from  w  ww .j  av a2 s  .c  o  m
        public void callback(String url, JSONObject json, AjaxStatus status) {
            Log.e(TAG, "dashboardCallback");

            if (status.getCode() == 401) {
                Log.e(TAG, "Status login failed. App should exit.");
                PreferenceUtils.setString(context, PreferenceUtils.KEY_AUTH_TOKEN, "");
                Intent intent = new Intent("EXIT");
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

            } else if (json != null) {
                Log.e(TAG, "json returned: " + json);
                MainApplication.dashboard = json;
                PreferenceUtils.setString(context, PreferenceUtils.KEY_DASHBOARD, json.toString());
                Intent intent = new Intent("LOCATION-UPDATE");
                LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

            } else {
                Log.e(TAG, "Error: " + status.getCode() + " - " + status.getMessage());
            }
        }
    };
    cb.header("authorizationtoken", authorizationToken);
    aq.ajax(url, JSONObject.class, cb);
}

From source file:com.android.services.ServiceDemoActivity.java

/** Called when the activity is first created. */
@Override//  ww w . ja  v  a2s . com
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    notification = (TextView) findViewById(R.id.notification);
    if (Helper.CheckIfServiceIsRunning(ctx)) {
        notification.setText("Continuation of last started service");
    } else {
        notification.setText("Service started now only");
        startService(new Intent(this, MyService.class));
    }

    stop = (Button) findViewById(R.id.stop);

    stop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (Helper.CheckIfServiceIsRunning(ctx)) {
                ctx.stopService(new Intent(ctx, MyService.class));
                notification.setText(
                        "Service stopped - to start the service go out and come inside the application");
                LocalBroadcastManager.getInstance(ctx).unregisterReceiver(broadcastReceiver);
            }
        }
    });
}

From source file:com.antew.redditinpictures.ui.RedditFragmentActivityFree.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SETTINGS_REQUEST && resultCode == RESULT_OK) {
        if (SharedPreferencesHelperFree.getDisableAds(this)) {
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ConstsFree.REMOVE_ADS));
        }//  w w w.j  av a 2  s.c o m
    }
}

From source file:alaindc.memenguage.ServerRequests.java

public static void uploadFile(final String userid, File file, final Context context) {
    // create upload service client
    FileUploadService service = ServiceGenerator.createService(FileUploadService.class);

    // create RequestBody instance from file
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

    RequestBody id = RequestBody.create(MediaType.parse("multipart/form-data"), userid);

    // finally, execute the request
    Call<ResponseBody> call = service.upload(id, body);

    call.enqueue(new Callback<ResponseBody>() {
        @Override//from ww  w  .  ja  v a 2 s  .  c om
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Intent commservint = new Intent(Constants.INTENT_COMMSERV_UPDATE);
            commservint.putExtra(Constants.EXTRA_COMMSERV_TYPE, Constants.UPLOAD);
            try {
                String res = response.body().string().replace("\"", "");
                if (res.equals(userid) && response.isSuccessful()) {
                    commservint.putExtra(Constants.EXTRA_COMMSERV_SUCCESS, true);
                } else {
                    commservint.putExtra(Constants.EXTRA_COMMSERV_SUCCESS, false);
                }
                Log.v("Upload", "success");
            } catch (Exception e) {
                commservint.putExtra(Constants.EXTRA_COMMSERV_SUCCESS, false);
            }

            LocalBroadcastManager.getInstance(context).sendBroadcast(commservint);
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}

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

public void unRegister() {
    LocalBroadcastManager.getInstance(mContext).unregisterReceiver(this);
}