Example usage for android.content Context sendOrderedBroadcast

List of usage examples for android.content Context sendOrderedBroadcast

Introduction

In this page you can find the example usage for android.content Context sendOrderedBroadcast.

Prototype

public abstract void sendOrderedBroadcast(@RequiresPermission Intent intent,
        @Nullable String receiverPermission);

Source Link

Document

Broadcast the given intent to all interested BroadcastReceivers, delivering them one at a time to allow more preferred receivers to consume the broadcast before it is delivered to less preferred receivers.

Usage

From source file:com.achep.acdisplay.services.media.MediaController2.java

/**
 * Emulates hardware buttons' click via broadcast system.
 *
 * @see android.view.KeyEvent/*from www .  j av a2 s.co  m*/
 */
public static void broadcastMediaAction(@NonNull Context context, int action) {
    int keyCode;
    switch (action) {
    case ACTION_PLAY_PAUSE:
        keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
        break;
    case ACTION_STOP:
        keyCode = KeyEvent.KEYCODE_MEDIA_STOP;
        break;
    case ACTION_SKIP_TO_NEXT:
        keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
        break;
    case ACTION_SKIP_TO_PREVIOUS:
        keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
        break;
    default:
        throw new IllegalArgumentException();
    }

    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    KeyEvent keyDown = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
    KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, keyCode);

    context.sendOrderedBroadcast(intent.putExtra(Intent.EXTRA_KEY_EVENT, keyDown), null);
    context.sendOrderedBroadcast(intent.putExtra(Intent.EXTRA_KEY_EVENT, keyUp), null);
}

From source file:com.tapchatapp.android.client.model.Connection.java

private void startBufferActivity(Buffer buffer) {
    Context appContext = TapchatApp.get();

    Intent intent = new Intent(TapchatApp.ACTION_OPEN_BUFFER);
    intent.putExtra("cid", String.valueOf(buffer.getConnection().getId()));
    intent.putExtra("bid", String.valueOf(buffer.getId()));
    appContext.sendOrderedBroadcast(intent, null);
}

From source file:com.tapchatapp.android.service.GCMReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String messageType = mGCM.getMessageType(intent);
    if (!messageType.equals(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE)) {
        return;/*from   w w  w . java2  s.  c  o m*/
    }

    try {
        byte[] cipherText = Base64.decode(intent.getStringExtra("payload"), Base64.URL_SAFE | Base64.NO_WRAP);
        byte[] iv = Base64.decode(intent.getStringExtra("iv"), Base64.URL_SAFE | Base64.NO_WRAP);

        byte[] key = mPusherClient.getPushKey();
        if (key == null) {
            // I don't think this will ever happen
            throw new Exception("Received push notification before receiving decryption key.");
        }

        JSONObject message = new JSONObject(new String(decrypt(cipherText, key, iv), "UTF-8"));

        Intent broadcastIntent = new Intent(TapchatApp.ACTION_MESSAGE_NOTIFY);
        addExtras(broadcastIntent, message);
        context.sendOrderedBroadcast(broadcastIntent, null);
    } catch (Exception ex) {
        Log.e(TAG, "Error parsing push notification", ex);
    }
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static void notifyStatusBarColorChanged(final Context context, final int themeResource,
        final int actionBarColor, final int backgroundAlpha) {
    final Intent intent = new Intent("com.mohammadag.colouredstatusbar.ChangeStatusBarColor");
    //        if (isColoredActionBar(themeResource)) {
    //            intent.putExtra("status_bar_color", backgroundAlpha << 24 | accentColor);
    //        } else {
    //            if (isLightActionBar(themeResource)) {
    //                intent.putExtra("status_bar_color", backgroundAlpha << 24 | 0xFFDDDDDD);
    //            } else {
    //                intent.putExtra("status_bar_color", backgroundAlpha << 24 | 0xFF222222);
    //            }
    //        }//from  w  w  w  .  jav  a 2s . c  om
    //        if (isLightActionBar(themeResource)) {
    //            intent.putExtra("status_bar_icons_color", Color.DKGRAY);
    //        } else {
    //            intent.putExtra("status_bar_icons_color", Color.WHITE);
    //        }
    // Please note that these are not yet implemented!!!
    // You're free to include them in your code so that when they
    // are implemented, your app will work out of the box.
    intent.putExtra("navigation_bar_color", Color.BLACK);
    intent.putExtra("navigation_bar_icon_color", Color.WHITE);
    context.sendOrderedBroadcast(intent, null);
}

From source file:com.android.email.EmailNotificationController.java

private static void refreshNotificationsForAccountInternal(final Context context, final long accountId) {
    final Uri accountUri = EmailProvider.uiUri("uiaccount", accountId);

    final ContentResolver contentResolver = context.getContentResolver();

    final Cursor mailboxCursor = contentResolver.query(
            ContentUris.withAppendedId(EmailContent.MAILBOX_NOTIFICATION_URI, accountId), null, null, null,
            null);/*from   w w w.j a v a2 s  .  co m*/
    try {
        while (mailboxCursor.moveToNext()) {
            final long mailboxId = mailboxCursor.getLong(EmailContent.NOTIFICATION_MAILBOX_ID_COLUMN);
            if (mailboxId == 0)
                continue;

            final int unseenCount = mailboxCursor.getInt(EmailContent.NOTIFICATION_MAILBOX_UNSEEN_COUNT_COLUMN);

            final int unreadCount;
            // If nothing is unseen, clear the notification
            if (unseenCount == 0) {
                unreadCount = 0;
            } else {
                unreadCount = mailboxCursor.getInt(EmailContent.NOTIFICATION_MAILBOX_UNREAD_COUNT_COLUMN);
            }

            final Uri folderUri = EmailProvider.uiUri("uifolder", mailboxId);

            LogUtils.d(LOG_TAG, "Changes to account " + accountId + ", folder: " + mailboxId + ", unreadCount: "
                    + unreadCount + ", unseenCount: " + unseenCount);

            final Intent intent = new Intent(UIProvider.ACTION_UPDATE_NOTIFICATION);
            intent.setPackage(context.getPackageName());
            intent.setType(EmailProvider.EMAIL_APP_MIME_TYPE);

            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_ACCOUNT, accountUri);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_FOLDER, folderUri);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNREAD_COUNT, unreadCount);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNSEEN_COUNT, unseenCount);

            context.sendOrderedBroadcast(intent, null);
        }
    } finally {
        mailboxCursor.close();
    }
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void answerCall(boolean isSpeakon) {

    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // ??// w w  w  . j av  a2s. c o m
    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

        /*TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method mthAnswerCall;
        try {
        mthAnswerCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
        mthAnswerCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthAnswerCall.invoke(telMag, (Object[]) null);
        iTel.answerRingingCall();
                
        } catch (Exception e) {
        e.printStackTrace();
        }*/
        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", "AudioManager answer mode:" + audioManager.getMode() + " speaker mod:"
                    + String.valueOf(audioManager.isSpeakerphoneOn()));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    } else {
        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

    }
}

From source file:com.tealeaf.PushBroadcastReceiver.java

@Override
public void onReceive(final Context context, Intent in) {
    final TeaLeafOptions options = new TeaLeafOptions(context);
    final String appID = options.getAppID();

    if (!appID.equals(in.getStringExtra("appID"))) {
        return;/*from ww w.jav a2  s .c o  m*/
    }
    scheduledIntent = null;

    new Thread(new Runnable() {
        public void run() {
            HTTP http = new HTTP();
            Settings settings = new Settings(context);
            String format = options.getPushUrl();
            String url = String.format(format, appID, Device.getDeviceID(context, settings),
                    options.getBuildIdentifier());

            logger.log("{push} Polling for notifications on", url);
            Pair<String, Integer> result = http.getPush(URI.create(url));
            int timeout = options.getPushDelay();
            if (result != null) {
                timeout = result.second;
                if (timeout == -1) {
                    timeout = options.getPushDelay();
                }
                String json = result.first;
                logger.log("{push} Got push notification", json, "and will delay for", timeout,
                        "seconds before checking again");

                try {
                    JSONArray array = new JSONArray(json);
                    int len = array.length();
                    JSONArray notifications = new JSONArray(settings.getPushNotifications());

                    for (int i = 0; i < len; i++) {
                        JSONObject msg = array.getJSONObject(i);
                        if (msg.optBoolean("crossPromo", false)) {
                            // a cross-promo
                            Intent intent = new Intent("com.tealeaf.CROSS_PROMO");
                            intent.putExtra("appid", msg.getString("appID"));
                            intent.putExtra("url", msg.getString("url"));
                            intent.putExtra("version", msg.getString("version"));
                            intent.putExtra("displayName", msg.getString("displayName"));
                            intent.putExtra("buildID", msg.getString("buildIdentifier"));
                            intent.putExtra("image", msg.optString("image"));
                            context.startService(intent);
                        } else if (msg.optBoolean("hasUpdate", false) && !settings.is("updating_now")) {
                            logger.log("{push} Got an update request (old build id:",
                                    options.getBuildIdentifier(), ", new build identifier:",
                                    msg.getString("buildIdentifier"), ", old android version:",
                                    options.getGameHash(), ", new android version:", msg.getString("gameHash"),
                                    ")");
                            // an update notification
                            Intent intent = new Intent("com.tealeaf.PERFORM_UPDATE");
                            // required parameters
                            // is the update supposed to be silent?
                            intent.putExtra("silent", msg.getBoolean("silent"));
                            // does the update require hitting the marketplace instead? (native runtime update)
                            intent.putExtra("market", !msg.getString("gameHash").equals(options.getGameHash()));
                            // what's the new build identifier of the update?
                            intent.putExtra("buildIdentifier", msg.getString("buildIdentifier"));
                            // what url can I download the updated build from?
                            intent.putExtra("url", msg.getString("url"));
                            context.sendOrderedBroadcast(intent, null);
                        }
                    }
                    settings.setPushNotifications(notifications.toString());
                } catch (JSONException e) {
                    logger.log(e);
                }
            }
            scheduleNext(context, timeout);
        }
    }).start();
}

From source file:com.tct.email.NotificationController.java

private static void refreshNotificationsForAccountInternal(final Context context, final long accountId) {
    final Uri accountUri = EmailProvider.uiUri("uiaccount", accountId);

    final ContentResolver contentResolver = context.getContentResolver();

    final Cursor mailboxCursor = contentResolver.query(
            ContentUris.withAppendedId(EmailContent.MAILBOX_NOTIFICATION_URI, accountId), null, null, null,
            null);//from   w  w  w.ja va  2 s  .  co  m
    try {
        while (mailboxCursor.moveToNext()) {
            final long mailboxId = mailboxCursor.getLong(EmailContent.NOTIFICATION_MAILBOX_ID_COLUMN);
            if (mailboxId == 0)
                continue;

            final int unseenCount = mailboxCursor.getInt(EmailContent.NOTIFICATION_MAILBOX_UNSEEN_COUNT_COLUMN);

            final int unreadCount;
            // If nothing is unseen, clear the notification
            if (unseenCount == 0) {
                unreadCount = 0;
            } else {
                unreadCount = mailboxCursor.getInt(EmailContent.NOTIFICATION_MAILBOX_UNREAD_COUNT_COLUMN);
            }
            //TS:zheng.zou 2015-12-17 EMAIL BUGFIX_861247  ADD_S
            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
            //TS:kaifeng.lu 2015-12-18 EMAIL BUGFIX_1190892  MOD_S
            if (mailbox != null && mailbox.mType == Mailbox.TYPE_INBOX) {
                //TS:kaifeng.lu 2015-12-18 EMAIL BUGFIX_1190892  MOD_E
                final Cursor unreadCursor = contentResolver.query(ContentUris
                        .withAppendedId(EmailContent.MAILBOX_MOST_RECENT_UNREAD_MESSAGE_URI, mailboxId), null,
                        null, null, null);
                long mostRecentUnreadMsgId = 0;
                if (unreadCursor != null && unreadCursor.moveToFirst()) {
                    try {
                        mostRecentUnreadMsgId = unreadCursor
                                .getLong(EmailContent.MAILBOX_MOST_RECENT_UNREAD_ID_COULUM);
                    } finally {
                        unreadCursor.close();
                    }
                }
                final int key = getUnreadKey(accountId, mailboxId);
                long lastMostRecentUnreadMsgId = sLastUnreadIds.get(key);
                LogUtils.i(LOG_TAG,
                        "key=" + key + " unseenCount=" + unseenCount + " unreadCount=" + unreadCount
                                + " lastMostRecentUnreadMsgId = " + lastMostRecentUnreadMsgId
                                + " mostRecentUnreadMsgId=" + mostRecentUnreadMsgId);
                //no need to send notification if latest Unread id not change
                if (lastMostRecentUnreadMsgId != 0 && unseenCount != 0
                        && lastMostRecentUnreadMsgId == mostRecentUnreadMsgId) {
                    LogUtils.i(LOG_TAG, "no need to send notification broadcast, continue");
                    continue;
                }
                sLastUnreadIds.put(key, mostRecentUnreadMsgId);
            }
            //TS:zheng.zou 2015-12-17 EMAIL BUGFIX_861247  ADD_E

            final Uri folderUri = EmailProvider.uiUri("uifolder", mailboxId);

            LogUtils.d(LOG_TAG, "Changes to account " + accountId + ", folder: " + mailboxId + ", unreadCount: "
                    + unreadCount + ", unseenCount: " + unseenCount);

            final Intent intent = new Intent(UIProvider.ACTION_UPDATE_NOTIFICATION);
            intent.setPackage(context.getPackageName());
            intent.setType(EmailProvider.EMAIL_APP_MIME_TYPE);

            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_ACCOUNT, accountUri);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_FOLDER, folderUri);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNREAD_COUNT, unreadCount);
            intent.putExtra(UIProvider.UpdateNotificationExtras.EXTRA_UPDATED_UNSEEN_COUNT, unseenCount);

            context.sendOrderedBroadcast(intent, null);
        }
    } finally {
        mailboxCursor.close();
    }
}