Example usage for android.content Context sendBroadcast

List of usage examples for android.content Context sendBroadcast

Introduction

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

Prototype

public abstract void sendBroadcast(@RequiresPermission Intent intent);

Source Link

Document

Broadcast the given intent to all interested BroadcastReceivers.

Usage

From source file:com.menumomma.chrome2phone.DeviceRegistrar.java

public static void registerWithServer(final Context context, final String deviceRegistrationID) {
    new Thread(new Runnable() {
        public void run() {
            Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
            try {
                HttpResponse res = makeRequest(context, deviceRegistrationID, REGISTER_PATH);
                if (res.getStatusLine().getStatusCode() == 200) {
                    SharedPreferences settings = Prefs.get(context);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("deviceRegistrationID", deviceRegistrationID);
                    editor.commit();//w  w w.j  a  v a  2  s .c  o  m
                    updateUIIntent.putExtra(STATUS_EXTRA, REGISTERED_STATUS);
                } else if (res.getStatusLine().getStatusCode() == 400) {
                    updateUIIntent.putExtra(STATUS_EXTRA, AUTH_ERROR_STATUS);
                } else {
                    Log.w(TAG, "Registration error " + String.valueOf(res.getStatusLine().getStatusCode()));
                    updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                }
                context.sendBroadcast(updateUIIntent);
            } catch (AppEngineClient.PendingAuthException pae) {
                // Get setup activity to ask permission from user.
                Intent intent = new Intent(SetupActivity.AUTH_PERMISSION_ACTION);
                intent.putExtra("AccountManagerBundle", pae.getAccountManagerBundle());
                context.sendBroadcast(intent);
            } catch (Exception e) {
                Log.w(TAG, "Registration error " + e.getMessage());
                updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                context.sendBroadcast(updateUIIntent);
            }
        }
    }).start();
}

From source file:net.helff.wificonnector.WifiConnectivityReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "action: " + intent.getAction());

    if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        Intent locationIntent = new LocationIntent();
        context.sendBroadcast(locationIntent);
    }// w ww.  ja  va  2  s  .c  om

    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        Log.d(TAG, "got network_state_changed with detailed info: " + networkInfo == null ? "nothing"
                : networkInfo.getDetailedState().name());

        if (networkInfo != null && networkInfo.isConnected()
                && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean autoConnect = prefs.getBoolean("autoConnect", false);

            Log.d(TAG, "triggering WifiConnectivityService with autoConnect=" + autoConnect);

            int command = autoConnect ? WifiConnectivityService.COMMAND_AUTO_UNLOCK_CONNECTION
                    : WifiConnectivityService.COMMAND_CHECK_CONNECTION;
            Intent msgIntent = new Intent(context, WifiConnectivityService.class);
            msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND, command);
            context.startService(msgIntent);
        }

    }
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        Log.d(TAG, "triggering WifiConnectivityService state changed");

        Intent msgIntent = new Intent(context, WifiConnectivityService.class);
        msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND,
                WifiConnectivityService.COMMAND_REFRESH_STATUS);
        context.startService(msgIntent);
    }
}

From source file:com.channelsoft.common.bitmapUtil.ImageWorker.java

public static void scanFileAsync(Context ctx, String filePath) {
    LogUtil.begin("filePath:" + filePath);
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    scanIntent.setData(Uri.fromFile(new File(filePath)));
    ctx.sendBroadcast(scanIntent);
}

From source file:org.umit.icm.mobile.social.TwitterUpdate.java

/**
 * Sends a Tweet using the Notification Service. 
 * //w w w.  j av  a  2  s . c  o  m
 * 
         
 @param   message      An object of type String which represents the Tweet.
 *
         
 @see Intent 
 */
public synchronized void sendTweet(String message, Context context) {
    Bundle bundle = new Bundle();
    bundle.putString("twitter", message);
    Intent intent = new Intent("org.umit.icm.mobile.TWITTER_SERVICE");
    intent.putExtras(bundle);
    context.sendBroadcast(intent);
}

From source file:com.google.android.apps.chrometophone.DeviceRegistrar.java

public static void unregisterWithServer(final Context context, final String deviceRegistrationID) {
    new Thread(new Runnable() {
        @Override/*from  w  w  w  . java2s .  c  o  m*/
        public void run() {
            Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
            try {
                HttpResponse res = makeRequest(context, deviceRegistrationID, UNREGISTER_PATH);
                if (res.getStatusLine().getStatusCode() != 200) {
                    Log.w(TAG, "Unregistration error " + String.valueOf(res.getStatusLine().getStatusCode()));
                } else {
                    GCMRegistrar.setRegisteredOnServer(context, false);
                }
            } catch (Exception e) {
                Log.w(TAG, "Unregistration error " + e.getMessage());
            } finally {
                SharedPreferences settings = Prefs.get(context);
                SharedPreferences.Editor editor = settings.edit();
                editor.remove("accountName");
                editor.commit();
                updateUIIntent.putExtra(STATUS_EXTRA, UNREGISTERED_STATUS);
            }

            // Update dialog activity
            context.sendBroadcast(updateUIIntent);
        }
    }).start();
}

From source file:com.fvd.nimbus.MainActivity.java

public static void createShortcut(Context context) {

    Intent shortcutIntent = new Intent(context, MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Nimbus Clipper");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(context, R.drawable.app_icon));
    addIntent.putExtra("duplicate", false);
    addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    context.sendBroadcast(addIntent);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(addIntent);/*  w w w.  j av a 2  s .c o m*/
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.services.ChannelService.java

public static void createChannel(Context ctx) {
    String regId = getGCMRegId(ctx);
    if (regId.isEmpty()) {
        log.warn("regId is empty. It will get from GCM register");
        return;/*from w  ww . ja  v a2s. c o m*/
    }

    Account ac = AccountUtils.getAccount(ctx, false);
    AccountManager am = (AccountManager) ctx.getSystemService(Context.ACCOUNT_SERVICE);

    String userId;
    try {
        userId = am.getUserData(ac, JsonKeys.ID_STORED);
    } catch (Exception e) {
        log.error("Cannot get userId", e);
        return;
    }

    String json;
    try {
        json = registerInServer(ctx, regId, userId);
    } catch (Exception e) {
        log.error("Cannot send regId to server", e);
        return;
    }

    if (json == null) {
        log.error("Cannot get channelId");
        return;
    }

    try {
        JSONObject obj = new JSONObject(json);

        String channelId = obj.getString(JsonKeys.CHANNEL_ID);
        storeRegistrationId(ctx, regId, channelId);

        Intent i = new Intent(ConstantKeys.BROADCAST_SERVER_REGISTER);
        ctx.sendBroadcast(i);
    } catch (JSONException e) {
        log.error("Error processing register result", e);
    }

    ctx.startService(new Intent(ctx.getApplicationContext(), CommandGetService.class));
}

From source file:com.browsertophone.DeviceRegistrar.java

public static void unregisterWithServer(final Context context, final String deviceRegistrationID) {
    new Thread(new Runnable() {
        public void run() {
            Intent updateUIIntent = new Intent("com.browsertophone.btp.UPDATE_UI");
            try {
                HttpResponse res = makeRequest(context, deviceRegistrationID, UNREGISTER_PATH);
                if (res.getStatusLine().getStatusCode() == 200) {
                    SharedPreferences settings = Prefs.get(context);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.remove("deviceRegistrationID");
                    editor.commit();/*from w w  w.  ja v a 2  s  . c om*/
                    updateUIIntent.putExtra(STATUS_EXTRA, UNREGISTERED_STATUS);
                } else {
                    Log.w(TAG, "Unregistration error " + String.valueOf(res.getStatusLine().getStatusCode()));
                    updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                }
            } catch (Exception e) {
                updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
                Log.w(TAG, "Unegistration error " + e.getMessage());
            }

            // Update dialog activity
            context.sendBroadcast(updateUIIntent);
        }
    }).start();
}

From source file:com.dmbstream.android.util.Util.java

/**
 * <p>Broadcasts the given player state as the one being set.</p>
 *//*www . j  a v  a 2 s  .  com*/
public static void broadcastPlaybackStatusChange(Context context, PlayerState state) {
    Intent intent = new Intent(EVENT_PLAYSTATE_CHANGED);

    switch (state) {
    case STARTED:
        intent.putExtra("state", "play");
        break;
    case STOPPED:
        intent.putExtra("state", "stop");
        break;
    case PAUSED:
        intent.putExtra("state", "pause");
        break;
    case COMPLETED:
        intent.putExtra("state", "complete");
        break;
    default:
        return; // No need to broadcast.
    }

    context.sendBroadcast(intent);
}

From source file:info.guardianproject.netcipher.web.WebkitProxy.java

private static boolean sendProxyChangedIntent(Context ctx, String host, int port) {

    try {//from w  ww .  j av a2  s  .  co  m
        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        if (proxyPropertiesClass != null) {
            Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class);

            if (c != null) {
                c.setAccessible(true);
                Object properties = c.newInstance(host, port, null);

                Intent intent = new Intent(android.net.Proxy.PROXY_CHANGE_ACTION);
                intent.putExtra("proxy", (Parcelable) properties);
                ctx.sendBroadcast(intent);

            }

        }
    } catch (Exception e) {
        Log.e("ProxySettings", "Exception sending Intent ", e);
    } catch (Error e) {
        Log.e("ProxySettings", "Exception sending Intent ", e);
    }

    return false;

}