Example usage for android.app PendingIntent getBroadcast

List of usage examples for android.app PendingIntent getBroadcast

Introduction

In this page you can find the example usage for android.app PendingIntent getBroadcast.

Prototype

public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, @Flags int flags) 

Source Link

Document

Retrieve a PendingIntent that will perform a broadcast, like calling Context#sendBroadcast(Intent) Context.sendBroadcast() .

Usage

From source file:com.rjfun.cordova.sms.SMSPlugin.java

private PluginResult sendSMS(JSONArray addressList, String text, CallbackContext callbackContext) {
    Log.d(LOGTAG, ACTION_SEND_SMS);//from   ww w.  jav a 2 s.com
    if (this.cordova.getActivity().getPackageManager().hasSystemFeature("android.hardware.telephony")) {
        int n;
        if ((n = addressList.length()) > 0) {
            PendingIntent sentIntent = PendingIntent.getBroadcast((Context) this.cordova.getActivity(), (int) 0,
                    (Intent) new Intent("SENDING_SMS"), (int) 0);
            SmsManager sms = SmsManager.getDefault();
            for (int i = 0; i < n; ++i) {
                String address;
                if ((address = addressList.optString(i)).length() <= 0)
                    continue;
                sms.sendTextMessage(address, null, text, sentIntent, (PendingIntent) null);
            }
        } else {
            PendingIntent sentIntent = PendingIntent.getActivity((Context) this.cordova.getActivity(), (int) 0,
                    (Intent) new Intent("android.intent.action.VIEW"), (int) 0);
            Intent intent = new Intent("android.intent.action.VIEW");
            intent.putExtra("sms_body", text);
            intent.setType("vnd.android-dir/mms-sms");
            try {
                sentIntent.send(this.cordova.getActivity().getApplicationContext(), 0, intent);
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "OK"));
    } else {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "SMS is not supported"));
    }
    return null;
}

From source file:org.ametro.app.ApplicationEx.java

public void changeAlarmReceiverState(boolean enabled) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.cancel(pendingIntent);//  w  w  w. ja v  a2s. c o m
    if (enabled) {
        long interval = (GlobalSettings.getUpdatePeriod(this) == 900) ? AlarmManager.INTERVAL_FIFTEEN_MINUTES
                : AlarmManager.INTERVAL_DAY;
        alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + 1000 * 60 * 2, interval,
                pendingIntent);
    }
}

From source file:com.deepak.myclock.alarms.AlarmNotifications.java

public static void showMissedNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying missed notification for alarm instance: " + instance.mId);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String label = instance.mLabel;
    String alarmTime = AlarmUtils.getFormattedTime(context, instance.getAlarmTime());
    String contextText = instance.mLabel.isEmpty() ? alarmTime
            : context.getString(R.string.alarm_missed_text, alarmTime, label);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.alarm_missed_title)).setContentText(contextText)
            .setSmallIcon(R.drawable.stat_notify_alarm).setPriority(NotificationCompat.PRIORITY_HIGH);

    // Setup dismiss intent
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.setDeleteIntent(PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content intent
    Intent showAndDismiss = AlarmInstance.createIntent(context, AlarmStateManager.class, instance.mId);
    showAndDismiss.setAction(AlarmStateManager.SHOW_AND_DISMISS_ALARM_ACTION);
    notification.setContentIntent(PendingIntent.getBroadcast(context, instance.hashCode(), showAndDismiss,
            PendingIntent.FLAG_UPDATE_CURRENT));

    nm.cancel(instance.hashCode());/* w w w .ja v a2s . co  m*/
    nm.notify(instance.hashCode(), notification.build());
}

From source file:com.gzsll.downloads.DownloadNotification.java

private void updateCompletedNotification(Collection<DownloadInfo> downloads) {
    for (DownloadInfo download : downloads) {
        if (!isCompleteAndVisible(download)) {
            continue;
        }//from  ww w  .j  av a 2  s .  c o m

        long id = download.mId;
        String title = download.mTitle;
        if (title == null || title.length() == 0) {
            title = mContext.getResources().getString(R.string.download_unknown_title);
        }
        Uri contentUri = ContentUris.withAppendedId(Downloads.ALL_DOWNLOADS_CONTENT_URI, id);
        String caption;
        Intent intent;
        if (Downloads.isStatusError(download.mStatus)) {
            caption = mContext.getResources().getString(R.string.notification_download_failed);
            intent = new Intent(Constants.ACTION_LIST);
        } else {
            caption = mContext.getResources().getString(R.string.notification_download_complete);
            if (download.mDestination == Downloads.DESTINATION_EXTERNAL) {
                intent = new Intent(Constants.ACTION_OPEN);
            } else {
                intent = new Intent(Constants.ACTION_LIST);
            }
        }
        intent.setClassName(mContext.getPackageName(), DownloadReceiver.class.getName());
        intent.setData(contentUri);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
        mBuilder.setContentTitle(title);
        mBuilder.setContentText(caption);
        mBuilder.setWhen(download.mLastMod);
        mBuilder.setContentIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));
        intent = new Intent(Constants.ACTION_HIDE);
        intent.setClassName(mContext.getPackageName(), DownloadReceiver.class.getName());
        intent.setData(contentUri);
        mBuilder.setDeleteIntent(PendingIntent.getBroadcast(mContext, 0, intent, 0));

        mSystemFacade.postNotification(download.mId, mBuilder.build());
    }
}

From source file:com.adstrosoftware.notificationcompass.CompassService.java

/**
 * TODO/*from  ww  w . j a v  a2s . c  o  m*/
 * 
 * @return
 */
private Notification buildNotification(int textResId, int iconResId, double azimuth) {
    Notification notification;

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent stopServiceIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_STOP_SERVICE),
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (!actionAdded) {
        notification = notificationBuilder.setContentTitle(getString(textResId))
                .setContentText(String.valueOf(azimuth)).setSmallIcon(iconResId).setOngoing(true)
                .setWhen(System.currentTimeMillis()).setContentIntent(contentIntent)
                .addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.stop),
                        stopServiceIntent)
                .build();

        actionAdded = true;
    } else {
        notification = notificationBuilder.setContentTitle(getString(textResId))
                .setContentText(String.valueOf(azimuth)).setSmallIcon(iconResId).setOngoing(true)
                .setWhen(System.currentTimeMillis()).setContentIntent(contentIntent).build();
    }

    return notification;
}

From source file:com.embeddedlog.LightUpDroid.alarms.AlarmNotifications.java

public static void showMissedNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying missed notification for alarm instance: " + instance.mId);
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String label = instance.mLabel;
    String alarmTime = AlarmUtils.getFormattedTime(context, instance.getAlarmTime());
    String contextText = instance.mLabel.isEmpty() ? alarmTime
            : context.getString(R.string.alarm_missed_text, alarmTime, label);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.alarm_missed_title)).setContentText(contextText)
            .setSmallIcon(R.drawable.stat_notify_alarm).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM);

    // Setup dismiss intent
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.setDeleteIntent(PendingIntent.getBroadcast(context, instance.hashCode(), dismissIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content intent
    Intent showAndDismiss = AlarmInstance.createIntent(context, AlarmStateManager.class, instance.mId);
    showAndDismiss.setAction(AlarmStateManager.SHOW_AND_DISMISS_ALARM_ACTION);
    notification.setContentIntent(PendingIntent.getBroadcast(context, instance.hashCode(), showAndDismiss,
            PendingIntent.FLAG_UPDATE_CURRENT));

    nm.cancel(instance.hashCode());// ww w  .j a  v a  2  s.  co  m
    nm.notify(instance.hashCode(), notification.build());
}

From source file:com.better.alarm.presenter.alert.AlarmAlertReceiver.java

private void onSoundExpired(int id) {
    Intent dismissAlarm = new Intent(mContext, AlarmAlertReceiver.class);
    dismissAlarm.setAction(ACTION_CANCEL_NOTIFICATION);
    dismissAlarm.putExtra(Intents.EXTRA_ID, id);
    PendingIntent intent = PendingIntent.getBroadcast(mContext, id, dismissAlarm, 0);
    // Update the notification to indicate that the alert has been
    // silenced.//from  w w w .  jav a  2 s. c om
    String label = alarm.getLabelOrDefault(mContext);
    int autoSilenceMinutes = Integer
            .parseInt(PreferenceManager.getDefaultSharedPreferences(mContext).getString("auto_silence", "10"));
    String text = mContext.getString(R.string.alarm_alert_alert_silenced, autoSilenceMinutes);

    Notification.Builder nb = new Notification.Builder(mContext);
    nb.setAutoCancel(true);
    nb.setSmallIcon(R.drawable.stat_notify_alarm);
    nb.setWhen(Calendar.getInstance().getTimeInMillis());
    nb.setContentIntent(intent);
    nb.setContentTitle(label);
    nb.setContentText(text);
    nb.setTicker(text);
    Notification n = nb.getNotification();

    // We have to cancel the original notification since it is in the
    // ongoing section and we want the "killed" notification to be a plain
    // notification.
    nm.cancel(id);
    nm.notify(id, n);
}

From source file:com.android.mms.transaction.NotificationTransaction.java

public void run() {
    MmsLog.d(MmsApp.TXN_TAG, "NotificationTransaction: run");
    DownloadManager downloadManager = DownloadManager.getInstance();
    boolean autoDownload = allowAutoDownload(mContext, mSubId);
    try {//from  ww w.  j a  v  a2  s  . c  om
        if (LOCAL_LOGV) {
            Log.v(TAG, "Notification transaction launched: " + this);
        }

        // By default, we set status to STATUS_DEFERRED because we
        // should response MMSC with STATUS_DEFERRED when we cannot
        // download a MM immediately.
        int status = STATUS_DEFERRED;
        // Don't try to download when data is suspended, as it will fail, so defer download
        if (!autoDownload) {
            // M: change API for ALPS01889178, use sub id.
            downloadManager.markState(mUri, DownloadManager.STATE_UNSTARTED, mSubId);
            sendNotifyRespInd(status);
            getState().setState(SUCCESS);
            getState().setContentUri(mUri);
            notifyObservers();
            return;
        }

        // M: change API for ALPS01889178, use sub id.
        downloadManager.markState(mUri, DownloadManager.STATE_DOWNLOADING, mSubId);

        if (mOpNotificationTransactionExt.run(mIsCancelling, mUri, mContext, getUri(), mContentLocation)) {
            mTransactionState.setState(TransactionState.SUCCESS);
            mTransactionState.setContentUri(mUri);
            mIsCancelling = false;
            return;
        }

        if (LOCAL_LOGV) {
            Log.v(TAG, "Content-Location: " + mContentLocation);
        }
        mPduFile = createPduFile(null, RETRIEVE_RESULT_NAME + mUri.getLastPathSegment());
        mPduFile.setWritable(true, false);

        //Intent intent = new Intent(TransactionService.ACTION_TRANSACION_PROCESSED);
        //intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, mSubId);
        //intent.putExtra(TransactionBundle.URI, mUri.toString());

        Log.d(MmsApp.TXN_TAG, "NotificationTransaction mUri:" + mUri);
        final Intent intent = new Intent(TransactionService.ACTION_TRANSACION_PROCESSED, mUri, mContext,
                MmsReceiver.class);
        intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, mSubId);

        PendingIntent downloadedIntent = PendingIntent.getBroadcast(mContext, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        SmsManager manager = SmsManager.getSmsManagerForSubscriptionId(mSubId);
        Log.d(MmsApp.TXN_TAG, "download MMS with param, mContentLocation = " + mContentLocation + ", mUri = "
                + mUri + ", subId" + mSubId);

        /// M: Add MmsService configure param @{
        Uri pduFileUri = FileProvider.getUriForFile(mContext, MMS_FILE_PROVIDER_AUTHORITIES, mPduFile);
        manager.downloadMultimediaMessage(mContext, mContentLocation, pduFileUri,
                MmsConfig.getMmsServiceConfig(), downloadedIntent);
        /// @}

        // sendNotifyRespInd(status);

        // Make sure this thread isn't over the limits in message count.
        Recycler.getMmsRecycler().deleteOldMessagesInSameThreadAsMessage(mContext, mUri);
        MmsWidgetProvider.notifyDatasetChanged(mContext);
    } catch (Throwable t) {
        getState().setState(FAILED);
        getState().setContentUri(mUri);
        notifyObservers();
        Log.e(TAG, Log.getStackTraceString(t));
    }
}

From source file:com.embeddedlog.LightUpDroid.timer.TimerReceiver.java

private void updateNextTimesup(Context context) {
    TimerObj t = getNextRunningTimer(mTimers, false, Utils.getTimeNow());
    long nextTimesup = (t == null) ? -1 : t.getTimesupTime();
    int timerId = (t == null) ? -1 : t.mTimerId;

    Intent intent = new Intent();
    intent.setAction(Timers.TIMES_UP);/*from  w  w w  .  j a v  a2 s.  co m*/
    intent.setClass(context, TimerReceiver.class);
    if (!mTimers.isEmpty()) {
        intent.putExtra(Timers.TIMER_INTENT_EXTRA, timerId);
    }
    AlarmManager mngr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent p = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
    if (t != null) {
        if (Utils.isKitKatOrLater()) {
            mngr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        } else {
            mngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        }
        if (Timers.LOGGING) {
            Log.d(TAG, "Setting times up to " + nextTimesup);
        }
    } else {
        mngr.cancel(p);
        if (Timers.LOGGING) {
            Log.v(TAG, "no next times up");
        }
    }
}

From source file:com.b44t.messenger.MusicPlayerService.java

public void setListeners(RemoteViews view) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
            new Intent(NOTIFY_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_previous, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_CLOSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_close, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PAUSE),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_pause, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_NEXT),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_next, pendingIntent);

    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(NOTIFY_PLAY),
            PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.player_play, pendingIntent);
}