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:be.uhasselt.privacypolice.NotificationHandler.java

/**
 * Asks the user whether it is certain that a network should be currently available
 * @param SSID The name of the network/*w w w . java 2 s  . co  m*/
 * @param BSSID The MAC address of the access point that triggered this (only used when we will block the AP)
 */
public void askNetworkPermission(String SSID, String BSSID) {
    Log.d("PrivacyPolice", "Asking permission for " + SSID + " (" + BSSID + ")");
    // Intent that will be used when the user allows the network
    Intent addIntent = new Intent(ctx, PermissionChangeReceiver.class);
    addIntent.putExtra("SSID", SSID).putExtra("BSSID", BSSID).putExtra("enable", true);
    PendingIntent addPendingIntent = PendingIntent.getBroadcast(ctx, 0, addIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    // Intent that will be used when the user blocks the network
    Intent disableIntent = new Intent(ctx, PermissionChangeReceiver.class);
    disableIntent.putExtra("SSID", SSID).putExtra("BSSID", BSSID).putExtra("enable", false);
    PendingIntent disablePendingIntent = PendingIntent.getBroadcast(ctx, 1, disableIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    // Intent that will be used when the user's OS does not support notification actions
    Intent activityIntent = new Intent(ctx, AskPermissionActivity.class);
    activityIntent.putExtra("SSID", SSID).putExtra("BSSID", BSSID);
    PendingIntent activityPendingIntent = PendingIntent.getActivity(ctx, 2, activityIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Build the notification dynamically, based on the network name
    Resources res = ctx.getResources();
    String headerString = String.format(res.getString(R.string.permission_header), SSID);
    String permissionString = String.format(res.getString(R.string.ask_permission), SSID);
    String yes = res.getString(R.string.yes);
    String no = res.getString(R.string.no);

    // NotificationCompat makes sure that the notification will also work on Android <4.0
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.ic_notification).setPriority(Notification.PRIORITY_MAX) // To force it to be first in list (and thus, expand)
            .setContentTitle(headerString).setContentText(permissionString)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(permissionString))
            .setContentIntent(activityPendingIntent)
            .addAction(android.R.drawable.ic_delete, no, disablePendingIntent)
            .addAction(android.R.drawable.ic_input_add, yes, addPendingIntent);
    notificationManager.notify(0, mBuilder.build());
}

From source file:cl.chihau.holaauto.MyMessagingService.java

private void sendNotificationForConversation(int conversationId, String sender, String message,
        long timestamp) {
    // A pending Intent for reads
    PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId,
            getMessageReadIntent(conversationId), PendingIntent.FLAG_UPDATE_CURRENT);

    /// Add the code to create the UnreadConversation
    // Build a RemoteInput for receiving voice input in a Car Notification
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).build();

    // Building a Pending Intent for the reply action to trigger
    PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId,
            getMessageReplyIntent(conversationId), PendingIntent.FLAG_UPDATE_CURRENT);

    // Create the UnreadConversation and populate it with the participant name,
    // read and reply intents.
    NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
            sender).setLatestTimestamp(timestamp).setReadPendingIntent(readPendingIntent)
                    .setReplyAction(replyIntent, remoteInput);

    // Note: Add messages from oldest to newest to the UnreadConversation.Builder
    // Since we are sending a single message here we simply add the message.
    // In a real world application there could be multiple messages which should be ordered
    // and added from oldest to newest.
    unreadConversationBuilder.addMessage(message);

    /// End create UnreadConversation

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), canal)
            .setSmallIcon(R.drawable.notification_icon)
            //.setLargeIcon(BitmapFactory.decodeResource(
            //        getApplicationContext().getResources(), R.drawable.ic_launcher_background))
            .setContentText(message).setWhen(timestamp).setContentTitle(sender)
            .setContentIntent(readPendingIntent).setContentIntent(readPendingIntent)
            /// Extend the notification with CarExtender.
            .extend(new NotificationCompat.CarExtender()
                    .setUnreadConversation(unreadConversationBuilder.build()));
    /// End//from w  w  w.  ja  va2s . com

    Log.d(TAG, "Sending notification " + conversationId + " conversation: " + message);

    //NotificationManagerCompat.from(this)
    //        .notify(conversationId, builder.build());
    mostrarNotificacion(conversationId, builder.build());
}

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

public static void showLowPriorityNotification(Context context, AlarmInstance instance) {
    Log.v("Displaying low 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(false).setAutoCancel(false)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT).setCategory(NotificationCompat.CATEGORY_ALARM);

    // Setup up hide notification
    Intent hideIntent = AlarmStateManager.createStateChangeIntent(context, "DELETE_TAG", instance,
            AlarmInstance.HIDE_NOTIFICATION_STATE);
    notification.setDeleteIntent(PendingIntent.getBroadcast(context, instance.hashCode(), hideIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));

    // 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, DeskClock.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  www  .j  av a 2 s . c  o m
    nm.notify(instance.hashCode(), notification.build());
}

From source file:ca.rmen.android.networkmonitor.app.service.NetMonNotification.java

/**
 * A notification which has the following functionalities:
 * 1) Tapping on the notification opens the app in the main activity
 * 2) Tapping on the stop button of the notification stops the service
 * 3) Tapping on the logs button of the notification opens the log activity
 *///  w  ww  .  j a va  2s.  co  m
static Notification createOngoingNotification(Context context) {
    Log.v(TAG, "createNotification");
    context.registerReceiver(sDisableBroadcastReceiver, new IntentFilter(ACTION_DISABLE));
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setOngoing(true);
    builder.setSmallIcon(R.drawable.ic_stat_service_running);
    builder.setTicker(context.getString(R.string.service_notification_ticker));
    builder.setContentTitle(context.getString(R.string.app_name));
    builder.setContentText(context.getString(R.string.service_notification_text));
    builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT));
    builder.addAction(R.drawable.ic_action_stop, context.getString(R.string.service_notification_action_stop),
            PendingIntent.getBroadcast(context, 0, new Intent(ACTION_DISABLE),
                    PendingIntent.FLAG_CANCEL_CURRENT));
    builder.addAction(R.drawable.ic_action_logs, context.getString(R.string.service_notification_action_logs),
            PendingIntent.getActivity(context, 0, new Intent(context, LogActivity.class),
                    PendingIntent.FLAG_UPDATE_CURRENT));
    builder.setColor(context.getResources().getColor(R.color.netmon_color));
    return builder.build();
}

From source file:com.bhb27.isu.services.BootBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    // only run if the app has run before and main has extracted asserts
    String action = intent.getAction();
    boolean run_boot = Tools.getBoolean("run_boot", false, context);
    boolean rootAccess = Tools.rootAccess(context);
    if (Intent.ACTION_BOOT_COMPLETED.equals(action) && rootAccess && run_boot) {
        Log.d(TAG, " Started action " + action + " run_boot " + run_boot);

        if (Tools.getBoolean("prop_run", false, context) && Tools.getBoolean("apply_props", false, context))
            ContextCompat.startForegroundService(context, new Intent(context, PropsService.class));

        ContextCompat.startForegroundService(context, new Intent(context, BootService.class));

        if (Tools.getBoolean("apply_su", false, context) && Tools.SuVersionBool(Tools.SuVersion(context))) {

            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent serviceIntent = new Intent("com.bhb27.isu.services.SuServiceReceiver.RUN");
            serviceIntent.putExtra("RUN", 100);
            serviceIntent.setClass(context, SuServiceReceiver.class);
            serviceIntent.setAction("RUN");

            PendingIntent pi = PendingIntent.getBroadcast(context, 100, serviceIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                    + Integer.valueOf(Tools.readString("apply_su_delay", "0", context)), pi);
        }/*from  w  ww.j av a 2  s  .  co m*/

    } else
        Log.d(TAG, "Not Started action " + action + " rootAccess " + rootAccess + " run_boot " + run_boot);
}

From source file:com.commonsware.android.deepbg.PollReceiver.java

static boolean doesPendingIntentExist(Context ctxt) {
    return (PendingIntent.getBroadcast(ctxt, 0, buildBaseIntent(ctxt), PendingIntent.FLAG_NO_CREATE) != null);
}

From source file:cat.wuyingren.whatsannoy.utils.SystemUtils.java

public static long createAlarm(Context context, int frequency, int alarmID) {
    Log.w("UTILS", "createAlarm()");
    Intent alarmIntent = new Intent(context, Alarm.class);
    alarmIntent.putExtra(Alarm.PREF_ALARM_ID, alarmID);
    PendingIntent pi = PendingIntent.getBroadcast(context, -1, alarmIntent, 0);

    Random r = new Random();
    int addMin = r.nextInt(frequency);
    if (addMin < 1) {
        addMin = 1;/*from   ww  w.  j  a  va  2  s  .  c  om*/
    }
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, addMin);
    c.add(Calendar.SECOND, 0);
    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
    return c.getTimeInMillis();
}

From source file:com.money.manager.ex.sync.SyncSchedulerBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = "";
    // by default, the action is ACTION_START. This is assumed on device boot.

    if (intent != null && !TextUtils.isEmpty(intent.getAction())) {
        action = intent.getAction();//  w  ww.java  2  s.  c  om
        Timber.d("Sync scheduler request: %s", action);
    }

    Intent syncIntent = new Intent(context, SyncBroadcastReceiver.class);
    PendingIntent pendingSyncIntent = PendingIntent.getBroadcast(context, SyncConstants.REQUEST_PERIODIC_SYNC,
            syncIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // PendingIntent.FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT

    AlarmManager alarmManager = getAlarmManager(context);

    // Cancel existing heartbeat.
    if (action.equals(ACTION_STOP)) {
        Timber.d("Stopping synchronization alarm.");

        alarmManager.cancel(pendingSyncIntent);
        return;
    }

    startHeartbeat(context, alarmManager, pendingSyncIntent);
}

From source file:br.com.virtz.www.cfcmob.MyMessagingService.java

private void sendNotification(int conversationId, String message, String participant, long timestamp) {
    // A pending Intent for reads
    PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId,
            createIntent(conversationId, READ_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);

    // Build a RemoteInput for receiving voice input in a Car Notification
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("Reply by voice").build();

    // Building a Pending Intent for the reply action to trigger
    PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId,
            createIntent(conversationId, REPLY_ACTION), PendingIntent.FLAG_UPDATE_CURRENT);

    // Create the UnreadConversation and populate it with the participant name,
    // read and reply intents.
    UnreadConversation.Builder unreadConvBuilder = new UnreadConversation.Builder(participant)
            .setLatestTimestamp(timestamp).setReadPendingIntent(readPendingIntent)
            .setReplyAction(replyIntent, remoteInput);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
            // Set the application notification icon:
            //.setSmallIcon(R.drawable.notification_icon)

            // Set the large icon, for example a picture of the other recipient of the message
            //.setLargeIcon(personBitmap)

            .setContentText(message).setWhen(timestamp).setContentTitle(participant)
            .setContentIntent(readPendingIntent)
            .extend(new CarExtender().setUnreadConversation(unreadConvBuilder.build()));

    mNotificationManager.notify(conversationId, builder.build());
}

From source file:com.jhone.demo.utils.NotificationUtils.java

/**
 * /*from   ww  w.  ja  va2 s .  c  om*/
 *
 * @param id              ID
 * @param activityIntent  Activity
 * @param broadcastIntent Broadcast
 * @param stateBar        ?
 * @param title           
 * @param content         
 * @param iconResId       
 * @param progress        ?
 * @return {@link NotificationCompat.Builder}
 */
public NotificationCompat.Builder createNotification(int id, Intent activityIntent, Intent broadcastIntent,
        CharSequence stateBar, CharSequence title, CharSequence content, int iconResId, int progress) {
    Context context = AppApplication.getInstance();
    mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(iconResId);
    builder.setTicker(stateBar);// ????
    builder.setContentTitle(title);// 
    builder.setContentText(content);// 
    builder.setOngoing(false);
    builder.setAutoCancel(false);
    builder.setProgress(100, progress, false);
    // 
    PendingIntent pendIntent;
    if (activityIntent != null) {
        pendIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
    } else {
        pendIntent = PendingIntent.getBroadcast(context, 0, broadcastIntent, 0);
    }
    builder.setContentIntent(pendIntent); // ?PendingIntent
    mNotifyManager.notify(id, builder.build());
    return builder;
}