Java Code Examples for android.app.Activity#sendBroadcast()

The following examples show how to use android.app.Activity#sendBroadcast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From JumpGo with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates a shortcut on the homescreen using the
 * {@link HistoryItem} information that opens the
 * browser. The icon, URL, and title are used in
 * the creation of the shortcut.
 *
 * @param activity the activity needed to create
 *                 the intent and show a snackbar message
 * @param item     the HistoryItem to create the shortcut from
 */
public static void createShortcut(@NonNull Activity activity, @NonNull HistoryItem item) {
    if (TextUtils.isEmpty(item.getUrl())) {
        return;
    }
    Log.d(TAG, "Creating shortcut: " + item.getTitle() + ' ' + item.getUrl());
    Intent shortcutIntent = new Intent(activity, MainActivity.class);
    shortcutIntent.setData(Uri.parse(item.getUrl()));

    final String title = TextUtils.isEmpty(item.getTitle()) ? activity.getString(R.string.untitled) : item.getTitle();

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, item.getBitmap());
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    activity.sendBroadcast(addIntent);
    Utils.showSnackbar(activity, R.string.message_added_to_homescreen);
}
 
Example 2
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 6 votes vote down vote up
/**
 * Called when a nearby endpoint is lost
 */
private void endpointLost(final String serviceId, final String endpointId) {
	final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);

	String endpointName = endpoint.getName();
	
	// Broadcast endpoint discovered
	Intent i = new Intent("com.butchmarshall.reactnative.google.nearby.connection.EndPointLost");
	Bundle bundle = new Bundle();
	bundle.putString("endpointId", endpointId);
	bundle.putString("serviceId", serviceId);
	bundle.putString("endpointName", endpointName);
	i.putExtras(bundle);

	final Activity activity = getCurrentActivity();
	activity.sendBroadcast(i);
}
 
Example 3
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 6 votes vote down vote up
/**
 * Called when a connection to a nearby advertiser is initiated
 */
private void connectionInitiatedToEndpoint(final String serviceId, final String endpointId, final ConnectionInfo connectionInfo) {
	final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);

	String authenticationToken = connectionInfo.getAuthenticationToken();
	String endpointName = connectionInfo.getEndpointName();
	Boolean incomingConnection = connectionInfo.isIncomingConnection();

	// Broadcast endpoint discovered
	Intent i = new Intent("com.butchmarshall.reactnative.google.nearby.connection.ConnectionInitiatedToEndpoint");
	Bundle bundle = new Bundle();
	bundle.putString("endpointId", endpointId);
	bundle.putString("endpointName", endpointName);
	bundle.putString("serviceId", serviceId);
	bundle.putString("authenticationToken", authenticationToken);
	bundle.putBoolean("incomingConnection", incomingConnection);
	i.putExtras(bundle);

	final Activity activity = getCurrentActivity();
	activity.sendBroadcast(i);
}
 
Example 4
Source File: ShortCutHelper.java    From Utils with Apache License 2.0 6 votes vote down vote up
/**
 * 为程序创建桌面快捷方式
 *
 * @param activity Activity
 * @param res      res
 */
public static void addShortcut(Activity activity, int res) {

    Intent shortcut = new Intent(
            "com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
            activity.getString(R.string.app_name));
    shortcut.putExtra("duplicate", false); // 不允许重复创建
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(activity, activity.getClass().getName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 快捷方式的图标
    ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
            activity, res);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    activity.sendBroadcast(shortcut);
}
 
Example 5
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 6 votes vote down vote up
/**
 * Called when connected to a nearby advertiser 
 */
private void connectedToEndpoint(final String serviceId, final String endpointId) {
	final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);

	logD(String.format("connectedToEndpoint(endpoint=%s)", endpoint));

	String endpointName = endpoint.getName();

	endpoint.setConnected(true);

	// Broadcast endpoint discovered
	Intent i = new Intent("com.butchmarshall.reactnative.google.nearby.connection.ConnectedToEndpoint");
	Bundle bundle = new Bundle();
	bundle.putString("endpointId", endpointId);
	bundle.putString("endpointName", endpointName);
	bundle.putString("serviceId", serviceId);
	i.putExtras(bundle);

	final Activity activity = getCurrentActivity();
	activity.sendBroadcast(i);
}
 
Example 6
Source File: ShortCutUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 为程序创建桌面快捷方式
 *
 * @param activity Activity
 */
public static void addShortcut(Activity activity) {
    Intent shortcut = new Intent(
            "com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
            activity.getString(R.string.app_name));
    shortcut.putExtra("duplicate", false); // 不允许重复创建
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(activity, activity.getClass().getName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 快捷方式的图标
    ShortcutIconResource iconRes = ShortcutIconResource.fromContext(
            activity, R.drawable.ic_launcher);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    activity.sendBroadcast(shortcut);
}
 
Example 7
Source File: DappBrowserFragment.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private boolean loadUrl(String urlText)
{
    detachFragments(true);
    addToBackStack(DAPP_BROWSER);
    cancelSearchSession();
    if (checkForMagicLink(urlText)) return true;
    web3.loadUrl(Utils.formatUrl(urlText), getWeb3Headers());
    urlTv.setText(Utils.formatUrl(urlText));
    web3.requestFocus();
    viewModel.setLastUrl(getContext(), urlText);
    Activity current = getActivity();
    if (current != null)
    {
        current.sendBroadcast(new Intent(RESET_TOOLBAR));
    }
    return true;
}
 
Example 8
Source File: GifUtils.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
public static void doNotifGif(File f, Activity c) {
    Intent mediaScanIntent =
            FileUtil.getFileIntent(f, new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE), c);
    c.sendBroadcast(mediaScanIntent);


    final Intent shareIntent = FileUtil.getFileIntent(f, new Intent(Intent.ACTION_VIEW), c);
    PendingIntent contentIntent =
            PendingIntent.getActivity(c, 0, shareIntent, PendingIntent.FLAG_CANCEL_CURRENT);


    Notification notif =
            new NotificationCompat.Builder(c).setContentTitle(c.getString(R.string.gif_saved))
                    .setSmallIcon(R.drawable.save_png)
                    .setContentIntent(contentIntent)
                    .setChannelId(Reddit.CHANNEL_IMG)
                    .build();

    NotificationManager mNotificationManager =
            (NotificationManager) c.getSystemService(Activity.NOTIFICATION_SERVICE);
    mNotificationManager.notify((int) System.currentTimeMillis(), notif);
}
 
Example 9
Source File: ShortCutUtils.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
/**
 * 为程序创建桌面快捷方式
 *
 * @param activity Activity
 * @param res      res
 */
public static void addShortcut(Activity activity, int res) {

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
    shortcut.putExtra("duplicate", false); // 不允许重复创建
    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName(activity, activity.getClass().getName());
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // 快捷方式的图标
    Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(activity, res);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

    activity.sendBroadcast(shortcut);
}
 
Example 10
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 6 votes vote down vote up
/**
 * Called when disconnected from an endpoint 
 */
private void disconnectedFromEndpoint(final String serviceId, final String endpointId) {
	final Endpoint endpoint = mEndpoints.get(serviceId+"_"+endpointId);
	
	if (endpoint == null) {
		logD("disconnectedFromEndpoint unknown endpoint");
		return;
	}
	logD(String.format("disconnectedFromEndpoint(endpoint=%s)", endpoint));

	String endpointName = endpoint.getName();

	endpoint.setConnected(false);

	// Broadcast endpoint discovered
	Intent i = new Intent("com.butchmarshall.reactnative.google.nearby.connection.DisconnectedFromEndpoint");
	Bundle bundle = new Bundle();
	bundle.putString("endpointId", endpointId);
	bundle.putString("endpointName", endpointName);
	bundle.putString("serviceId", serviceId);
	i.putExtras(bundle);

	final Activity activity = getCurrentActivity();
	activity.sendBroadcast(i);
}
 
Example 11
Source File: NearbyConnectionModule.java    From react-native-google-nearby-connection with MIT License 6 votes vote down vote up
/**
	 * Called when a payload was received
	 */
	private void onReceivePayload(final String serviceId, final String endpointId, Payload payload) {
		int payloadType = payload.getType();
		long payloadId = payload.getId();

		// Broadcast endpoint discovered
		Intent i = new Intent("com.butchmarshall.reactnative.google.nearby.connection.ReceivePayload");
		Bundle bundle = new Bundle();
		bundle.putString("serviceId", serviceId);
		bundle.putString("endpointId", endpointId);
		bundle.putInt("payloadType", payloadType);
		bundle.putString("payloadId", Long.toString(payloadId));
/*
		if (payloadType == Payload.Type.FILE) {
			long payloadSize = payload.getSize();
			bundle.putLong("payloadSize", payloadSize);
		}
*/
		i.putExtras(bundle);

		final Activity activity = getCurrentActivity();
		activity.sendBroadcast(i);
	}
 
Example 12
Source File: DictionarySettingsFragment.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public void onPause() {
    super.onPause();
    final Activity activity = getActivity();
    UpdateHandler.unregisterUpdateEventListener(this);
    activity.unregisterReceiver(mConnectivityChangedReceiver);
    if (mChangedSettings) {
        final Intent newDictBroadcast =
                new Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION);
        activity.sendBroadcast(newDictBroadcast);
        mChangedSettings = false;
    }
}
 
Example 13
Source File: DictionarySettingsFragment.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@Override
public void onPause() {
    super.onPause();
    final Activity activity = getActivity();
    UpdateHandler.unregisterUpdateEventListener(this);
    activity.unregisterReceiver(mConnectivityChangedReceiver);
    if (mChangedSettings) {
        Context context = activity.getApplicationContext();
        final Intent newDictBroadcast =
                new Intent(new DictionaryPackConstants(context).NEW_DICTIONARY_INTENT_ACTION);
        activity.sendBroadcast(newDictBroadcast);
        mChangedSettings = false;
    }
}
 
Example 14
Source File: ImagePicker.java    From androidnative.pri with Apache License 2.0 5 votes vote down vote up
private static void broadcastToMediaScanner(Uri uri) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(uri);

    Activity activity = org.qtproject.qt5.android.QtNative.activity();
    activity.sendBroadcast(mediaScanIntent);
}
 
Example 15
Source File: SnapshotManager.java    From evercam-android with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Notify Gallery about the snapshot that got saved, otherwise the image
 * won't show in Gallery
 *
 * @param path full snapshot path
 */
public static void updateGallery(String path, String cameraId, Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStoragePublicDirectory
                        (Environment.DIRECTORY_PICTURES))));
    } else {
        new SingleMediaScanner(activity).startScan(path, cameraId);
    }
}
 
Example 16
Source File: BackgroundLocationServicesPlugin.java    From cordova-background-geolocation-services with Apache License 2.0 5 votes vote down vote up
@Override
public void onResume(boolean multitasking) {
    if(debug()) {
        Log.d(TAG, "- locationUpdateReceiver Resumed (stopping recording)" + String.valueOf(isEnabled));
    }
    if (isEnabled) {
        Activity activity = this.cordova.getActivity();
        activity.sendBroadcast(new Intent(Constants.STOP_RECORDING));
    }
}
 
Example 17
Source File: DictionarySettingsFragment.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public void onPause() {
    super.onPause();
    final Activity activity = getActivity();
    UpdateHandler.unregisterUpdateEventListener(this);
    activity.unregisterReceiver(mConnectivityChangedReceiver);
    if (mChangedSettings) {
        final Intent newDictBroadcast =
                new Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION);
        activity.sendBroadcast(newDictBroadcast);
        mChangedSettings = false;
    }
}
 
Example 18
Source File: VideoStream.java    From cordova-rtmp-rtsp-stream with Apache License 2.0 5 votes vote down vote up
public static void sendBroadCast(Activity activity, String methodName, JSONObject object) {
    Intent intent = new Intent();
    intent.setAction(BROADCAST_LISTENER);
    intent.putExtra("method", methodName);
    intent.putExtra("data", object.toString());
    activity.sendBroadcast(intent);
}
 
Example 19
Source File: MainFragment.java    From continuous-audiorecorder with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Creates new item in the system's media database.
 *
 * @see <a href="https://github.com/android/platform_packages_apps_soundrecorder/blob/master/src/com/android/soundrecorder/SoundRecorder.java">Android Recorder source</a>
 */
public Uri saveCurrentRecordToMediaDB(final String fileName) {
    if (mAudioRecordUri != null) return mAudioRecordUri;

    final Activity activity = getActivity();
    final Resources res = activity.getResources();
    final ContentValues cv = new ContentValues();
    final File file = new File(fileName);
    final long current = System.currentTimeMillis();
    final long modDate = file.lastModified();
    final Date date = new Date(current);
    final String dateTemplate = res.getString(R.string.audio_db_title_format);
    final SimpleDateFormat formatter = new SimpleDateFormat(dateTemplate, Locale.getDefault());
    final String title = formatter.format(date);
    final long sampleLengthMillis = 1;
    // Lets label the recorded audio file as NON-MUSIC so that the file
    // won't be displayed automatically, except for in the playlist.
    cv.put(MediaStore.Audio.Media.IS_MUSIC, "0");

    cv.put(MediaStore.Audio.Media.TITLE, title);
    cv.put(MediaStore.Audio.Media.DATA, file.getAbsolutePath());
    cv.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    cv.put(MediaStore.Audio.Media.DATE_MODIFIED, (int) (modDate / 1000));
    cv.put(MediaStore.Audio.Media.DURATION, sampleLengthMillis);
    cv.put(MediaStore.Audio.Media.MIME_TYPE, "audio/*");
    cv.put(MediaStore.Audio.Media.ARTIST, res.getString(R.string.audio_db_artist_name));
    cv.put(MediaStore.Audio.Media.ALBUM, res.getString(R.string.audio_db_album_name));

    Log.d(TAG, "Inserting audio record: " + cv.toString());

    final ContentResolver resolver = activity.getContentResolver();
    final Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Log.d(TAG, "ContentURI: " + base);

    mAudioRecordUri = resolver.insert(base, cv);
    if (mAudioRecordUri == null) {
        return null;
    }
    activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mAudioRecordUri));
    return mAudioRecordUri;
}
 
Example 20
Source File: VideoStream.java    From cordova-rtmp-rtsp-stream with Apache License 2.0 4 votes vote down vote up
static void sendBroadCast(Activity activity, String methodName) {
    Intent intent = new Intent();
    intent.setAction(BROADCAST_LISTENER);
    intent.putExtra("method", methodName);
    activity.sendBroadcast(intent);
}