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.deepak.myclock.alarms.AlarmNotifications.java

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

    Resources resources = context.getResources();
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentTitle(resources.getString(R.string.alarm_alert_predismiss_title))
            .setContentText(AlarmUtils.getAlarmText(context, instance))
            .setSmallIcon(R.drawable.stat_notify_alarm).setOngoing(true).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    // Setup up dismiss action
    Intent dismissIntent = AlarmStateManager.createStateChangeIntent(context, "DISMISS_TAG", instance,
            AlarmInstance.DISMISSED_STATE);
    notification.addAction(android.R.drawable.ic_menu_close_clear_cancel,
            resources.getString(R.string.alarm_alert_dismiss_now_text), PendingIntent.getBroadcast(context,
                    instance.hashCode(), dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Setup content action if instance is owned by alarm
    long alarmId = instance.mAlarmId == null ? Alarm.INVALID_ID : instance.mAlarmId;
    Intent viewAlarmIntent = Alarm.createIntent(context, MainActivity.class, alarmId);
    //viewAlarmIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
    viewAlarmIntent.putExtra(AlarmClockFragment.SCROLL_TO_ALARM_INTENT_EXTRA, alarmId);
    viewAlarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setContentIntent(PendingIntent.getActivity(context, instance.hashCode(), viewAlarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

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

From source file:com.example.android.notifyme.MainActivity.java

/**
 * OnClick method for the "Notify Me!" button. Creates and delivers a simple notification
 *//*from  w ww.jav a2  s  .  c o  m*/
public void sendNotification() {

    //Sets up the pending intent that is delivered when the notification is clicked
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Sets up the pending intent to cancel the notification,
    // delivered when the user dismisses the notification
    Intent cancelIntent = new Intent(ACTION_CANCEL_NOTIFICATION);
    PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, NOTIFICATION_ID, cancelIntent,
            PendingIntent.FLAG_ONE_SHOT);

    //Sets up the pending intent associated with the Learn More notification action,
    //uses an implicit intent to go to the web.
    Intent learnMoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(NOTIFICATION_GUIDE_URL));
    PendingIntent learnMorePendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, learnMoreIntent,
            PendingIntent.FLAG_ONE_SHOT);

    //Sets up the pending intent to update the notification. Corresponds to a press of the
    //Update Me! button
    Intent updateIntent = new Intent(ACTION_UPDATE_NOTIFICATION);
    PendingIntent updatePendingIntent = PendingIntent.getBroadcast(this, NOTIFICATION_ID, updateIntent,
            PendingIntent.FLAG_ONE_SHOT);

    //Builds the notification with all of the parameters
    NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(getString(R.string.notification_text)).setSmallIcon(R.drawable.ic_android)
            .setContentIntent(notificationPendingIntent).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .addAction(R.drawable.ic_learn_more, getString(R.string.learn_more), learnMorePendingIntent)
            .addAction(R.drawable.ic_update, getString(R.string.update), updatePendingIntent)
            .setDeleteIntent(cancelPendingIntent);

    //Delivers the notification
    mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());

    //Enables the update and cancel buttons but disables the "Notify Me!" button
    mNotifyButton.setEnabled(false);
    mUpdateButton.setEnabled(true);
    mCancelButton.setEnabled(true);

}

From source file:jieehd.villain.updater.VillainUpdater.java

public void setRepeatingAlarm() {
    AlarmManager am;//w w  w  . ja  v  a 2  s .c  o  m
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, checkInBackground.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (3 * AlarmManager.INTERVAL_DAY),
            pendingIntent);
}

From source file:com.amaze.filemanager.asynchronous.services.ExtractService.java

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    String file = intent.getStringExtra(KEY_PATH_ZIP);
    String extractPath = intent.getStringExtra(KEY_PATH_EXTRACT);
    String[] entries = intent.getStringArrayExtra(KEY_ENTRIES_ZIP);

    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    accentColor = ((AppConfig) getApplication()).getUtilsProvider().getColorPreference()
            .getCurrentUserColorPreferences(this, sharedPreferences).accent;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.putExtra(MainActivity.KEY_INTENT_PROCESS_VIEWER, true);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    customSmallContentViews = new RemoteViews(getPackageName(), R.layout.notification_service_small);
    customBigContentViews = new RemoteViews(getPackageName(), R.layout.notification_service_big);

    Intent stopIntent = new Intent(TAG_BROADCAST_EXTRACT_CANCEL);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 1234, stopIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_zip_box_grey600_36dp,
            getString(R.string.stop_ftp), stopPendingIntent);

    mBuilder = new NotificationCompat.Builder(context, NotificationConstants.CHANNEL_NORMAL_ID);
    mBuilder.setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_zip_box_grey600_36dp)
            .setContentIntent(pendingIntent).setCustomContentView(customSmallContentViews)
            .setCustomBigContentView(customBigContentViews).setCustomHeadsUpContentView(customSmallContentViews)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle()).addAction(action).setOngoing(true)
            .setColor(accentColor);/*from ww  w.  jav a 2 s .c  o  m*/

    NotificationConstants.setMetadata(getApplicationContext(), mBuilder, NotificationConstants.TYPE_NORMAL);
    startForeground(NotificationConstants.EXTRACT_ID, mBuilder.build());
    initNotificationViews();

    long totalSize = getTotalSize(file);

    progressHandler.setSourceSize(1);
    progressHandler.setTotalSize(totalSize);
    progressHandler.setProgressListener((speed) -> publishResults(speed, false, false));

    super.onStartCommand(intent, flags, startId);
    super.progressHalted();
    new DoWork(this, progressHandler, file, extractPath, entries).execute();

    return START_STICKY;
}

From source file:com.DorsetEggs.waver.communicatorService.java

/**
 * The IntentService calls this method from the default worker thread with
 * the intent that started the service. When this method returns, IntentService
 * stops the service, as appropriate.// w  ww.ja  v a 2s  . c  o  m
 */
@Override
protected void onHandleIntent(Intent intent) {
    //Using the the flag to decide if this is a singleton
    sendDebugMessage("Intent handled, active == true");
    /*if(!active)*/
    {
        //android.os.Debug.waitForDebugger();
        callOngoing = false;
        sendDebugMessage("Waiting for connection");

        instantiateAnimation();

        mUsbManager = UsbManager.getInstance(this);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        IntentFilter usbFilter = new IntentFilter(ACTION_USB_PERMISSION);
        usbFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        registerReceiver(mUsbReceiver, usbFilter);

        IntentFilter callFilter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        registerReceiver(mCallReceiver, callFilter);

        if (mAccessory != null) {
            setConnectionStatus(true);
            return;
        }

        UsbAccessory[] accessories = mUsbManager.getAccessoryList();
        UsbAccessory accessory = (accessories == null ? null : accessories[0]);
        if (accessory != null) {
            sendDebugMessage("USB ready");
            if (mUsbManager.hasPermission(accessory)) {
                openAccessory(accessory);
                active = true;
                launchNotification(mId, "Indi connected");
                resetMotors();
                //Initialise the database
                SQLiteDatabase db = globals.dbHelper.getWritableDatabase();
            } else {
                setConnectionStatus(false);
                synchronized (mUsbReceiver) {
                    if (!mPermissionRequestPending) {
                        mUsbManager.requestPermission(accessory, mPermissionIntent);
                        mPermissionRequestPending = true;
                    }
                }
            }
        } else {
            setConnectionStatus(false);
            if (D)
                sendDebugMessage("mAccessory is null");
        }
        // Play here until the phone is disconnected
        while (true)
            ;
    }
}

From source file:app.jorge.mobile.com.transportalert.ScrollingActivity.java

public void scheduleAlarm() {
    // Construct an intent that will execute the AlarmReceiver
    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every 5 seconds
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY

    //alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
    //      AlarmManager.INTERVAL_HALF_HOUR, pIntent);

    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, AlarmManager.INTERVAL_HALF_HOUR, pIntent);
}

From source file:de.appplant.cordova.plugin.notification.Notification.java

/**
 * Schedule the local notification.// w  w  w.  j a  v  a2s  . c  o  m
 */
public void schedule() {
    long triggerTime = getNextTriggerTime();

    persist();

    // Intent gets called when the Notification gets fired
    Intent intent = new Intent(context, receiver).setAction(options.getId()).putExtra(Options.EXTRA,
            options.toString());

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}

From source file:com.commontime.plugin.notification.notification.NotificationWrapper.java

/**
 * Schedule the local notification.//from  w w w .j a v a  2  s . c o  m
 */
public void schedule() {
    long triggerTime = options.getTriggerTime();

    persist();

    // Intent gets called when the NotificationWrapper gets fired
    Intent intent = new Intent(context, receiver).setAction(options.getIdStr()).putExtra(Options.EXTRA,
            options.toString());

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    if (isRepeating()) {
        getAlarmMgr().setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getRepeatInterval(), pi);
    } else {
        getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
    }
}

From source file:com.abhijitvalluri.android.fitnotifications.widget.ServiceToggle.java

private PendingIntent getPendingSelfIntent(Context context, int appWidgetId, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);//from   w  w  w  . j  a v  a2 s  .  c  om
    return PendingIntent.getBroadcast(context, appWidgetId, intent, 0);
}

From source file:com.dpcsoftware.mn.App.java

public void setFlag(int tableId) {
    switch (tableId) {
    case 1:/*from  w w w.j a va  2s .  co m*/
        mnUpdateList = true;

        //Update widgets if anyone is being used
        AppWidgetManager wManager = AppWidgetManager.getInstance(this);
        ComponentName cWidgetProvider = new ComponentName(this, Widget1.class);
        int wIds[] = wManager.getAppWidgetIds(cWidgetProvider);
        if (wIds.length != 0) {
            Intent updateIntent = new Intent(this, Widget1.class);
            updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, wIds);
            try {
                PendingIntent.getBroadcast(this, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT).send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
        break;
    case 2:
        mnUpdateList = true;
        break;
    case 3:
        mnUpdateMenu = true;
        break;
    }
}