Example usage for android.app PendingIntent getActivity

List of usage examples for android.app PendingIntent getActivity

Introduction

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

Prototype

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

Source Link

Document

Retrieve a PendingIntent that will start a new activity, like calling Context#startActivity(Intent) Context.startActivity(Intent) .

Usage

From source file:com.google.android.gcm.demo.app.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from w  ww.j av a  2 s. c o m*/
private static void generateNotification(Context context, String message, String data) {
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(0);

    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, DemoActivity.class);
    notificationIntent.putExtra(CommonUtilities.EXTRA_TEAM_IN_JSON, data);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:com.example.pushtester.GcmIntentService.java

private void sendNotification(String msg) {
    Log.i(TAG, "sendNotification:" + msg);
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

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

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.makotosan.vimeodroid.TransferService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    counter++;/*from  www .j  a v  a  2 s.co m*/
    transfer = new Transfer();
    final String videoUri = intent.getStringExtra("videouri");
    final TransferType transferType = TransferType.valueOf(intent.getStringExtra("transferType"));
    final String fileName = intent.getStringExtra("fileName");
    Bitmap bitmapThumbnail = null;
    int transferIcon = android.R.drawable.stat_sys_download;

    if (transferType == TransferType.Upload) {
        // Get the resource ID for the video
        final long resourceId = ContentUris.parseId(Uri.parse(videoUri));

        bitmapThumbnail = android.provider.MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(),
                resourceId, android.provider.MediaStore.Video.Thumbnails.MICRO_KIND, null);
        transfer.setIcon(bitmapThumbnail);
        transferIcon = android.R.drawable.stat_sys_upload;
    }

    // Initialize our notification
    final Notification notification = new Notification(transferIcon, "Transferring...",
            System.currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(this.getPackageName(), R.layout.transferprogress);

    final Intent manageIntent = new Intent(this, ManageTransfersActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, manageIntent, 0);

    notification.contentIntent = pendingIntent;
    if (bitmapThumbnail != null) {
        notification.contentView.setImageViewBitmap(R.id.transferprogressIcon, bitmapThumbnail);
    } else {
        notification.contentView.setImageViewResource(R.id.transferprogressIcon, R.drawable.icon);
    }

    notification.contentView.setProgressBar(R.id.transferprogressBar, 100, 0, false);
    notification.contentView.setTextViewText(R.id.transferprogressText, "Transferring... ");
    notification.contentView.setOnClickPendingIntent(R.layout.transferprogress, pendingIntent);

    // Add it to the collection of notifications
    notifications.put(counter, notification);

    // Add our notification to the notification tray
    notificationManager.notify(counter, notification);

    // Initialize our asynchronous transfer task
    final TransferTask task = new TransferTask(counter, transferType);

    task.execute(videoUri, fileName);
    return Service.START_STICKY;
}

From source file:com.carpool.dj.carpool.model.GcmIntentService.java

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

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

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("GCM Notification").setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);//from w  ww. j a  v  a2  s. c  o  m

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.ushahidi.android.app.UshahidiService.java

private void showNotification(String tickerText) {

    // This is what should be launched if the user selects our notification.
    Intent baseIntent = new Intent(this, IncidentsTab.class);
    baseIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, baseIntent, 0);

    // choose the ticker text
    newUshahidiReportNotification = new Notification(R.drawable.favicon, tickerText,
            System.currentTimeMillis());
    newUshahidiReportNotification.contentIntent = contentIntent;
    newUshahidiReportNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newUshahidiReportNotification.defaults = Notification.DEFAULT_ALL;
    newUshahidiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent);

    if (UshahidiPref.ringtone) {
        // set the ringer
        Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3"));
        newUshahidiReportNotification.sound = ringURI;
    }//from w  w  w  .j a v a2 s . c o m

    if (UshahidiPref.vibrate) {
        double vibrateLength = 100 * Math.exp(0.53 * 20);
        long[] vibrate = new long[] { 100, 100, (long) vibrateLength };
        newUshahidiReportNotification.vibrate = vibrate;

        if (UshahidiPref.flashLed) {
            int color = Color.BLUE;
            newUshahidiReportNotification.ledARGB = color;
        }

        newUshahidiReportNotification.ledOffMS = (int) vibrateLength;
        newUshahidiReportNotification.ledOnMS = (int) vibrateLength;
        newUshahidiReportNotification.flags = newUshahidiReportNotification.flags
                | Notification.FLAG_SHOW_LIGHTS;
    }

    mNotificationManager.notify(UshahidiPref.NOTIFICATION_ID, newUshahidiReportNotification);
}

From source file:org.sigimera.app.android.GCMIntentService.java

@Override
protected final void onMessage(final Context context, final Intent message) {
    final Intent msg = message;
    this.mainThreadHandler.post(new Runnable() {
        public void run() {
            ApplicationController controller = ApplicationController.getInstance();
            controller.init(getApplicationContext(), getSharedPreferences(Constants.PREFS_NAME, 0), null);
            String authToken = controller.getSharedPreferences().getString("auth_token", null);
            final String type = msg.getStringExtra("sig_message_type");
            if (type.equalsIgnoreCase("NEW_CRISIS")) {
                /**/*from  w w w  .  j  a  v  a 2 s .c  o  m*/
                 * XXX: Blocks UI: Shift this code into a separate
                 * background thread
                 */
                Crisis crisis = PersistanceController.getInstance().getCrisis(authToken,
                        msg.getStringExtra("crisis_id"));

                StringBuffer message = new StringBuffer();
                if (crisis != null) {
                    message.append(crisis.getID());
                    message.append(" was stored successfully!");
                } else {
                    message.append("Not able to get crisis!");
                }
                Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_LONG).show();
            } else if (type.equalsIgnoreCase("PING")) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                Intent notificationIntent = new Intent();
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                        notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

                Notification notification = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("Sigimera PING!")
                        .setContentText("Congratulations, push notifcation received!")
                        .setSmallIcon(R.drawable.sigimera_logo).setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL).setContentIntent(contentIntent).build();

                mNotificationManager.notify(Constants.PING_ID, notification);
            } else if (type.equalsIgnoreCase("CRISIS_ALERT")) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                /**
                 * XXX: Not working with random ID. That makes always the
                 * latest notification clickable, but not the older ones.
                 */
                int notificationID = new Random().nextInt();
                Intent notificationIntent = new Intent(getApplicationContext(), CrisisAlertActivity.class);
                notificationIntent.putExtra("notification_id", notificationID);
                notificationIntent.putExtra("crisis_id", msg.getStringExtra("crisis_id"));
                notificationIntent.putExtra("crisis_type", msg.getStringExtra("crisis_type"));
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                        notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

                Notification notification = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("CRISIS ALERT!")
                        .setContentText("Crisis found: " + msg.getStringExtra("crisis_id"))
                        .setSmallIcon(R.drawable.alert_red).setOngoing(true).setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL).setContentIntent(contentIntent).build();

                mNotificationManager.notify(notificationID, notification);
            } else if (type.equalsIgnoreCase("SHARED_CRISIS")) {
                Intent intent = new Intent(GCMIntentService.this, CrisisActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Constants.CRISIS_ID, msg.getStringExtra("crisis_id"));
                intent.putExtra(Constants.WINDOW_TYPE, Constants.WINDOW_TYPE_SHARED_CRISIS);
                startActivity(intent);
            } else if (type.equalsIgnoreCase("UNREGISTER_DEVICE")) {
                GCMRegistrar.unregister(ApplicationController.getInstance().getApplicationContext());
            } else if (type.equalsIgnoreCase("REFRESH")) {
                LocationUpdaterHttpHelper locUpdater = new LocationUpdaterHttpHelper();
                Location loc = LocationController.getInstance().getLastKnownLocation();
                if (loc != null) {
                    String latitude = loc.getLatitude() + "";
                    String longitude = loc.getLongitude() + "";
                    if (authToken != null) {
                        locUpdater.execute(authToken, latitude, longitude);
                    }
                } else {
                    // TODO: Notify the user that the update location flow
                    // has not worked.
                }
            }
        }
    });
}

From source file:net.olejon.mdapp.MessageIntentService.java

@Override
protected void onHandleIntent(Intent intent) {
    final Context mContext = this;

    final MyTools mTools = new MyTools(mContext);

    RequestQueue requestQueue = Volley.newRequestQueue(mContext);

    int projectVersionCode = mTools.getProjectVersionCode();

    String device = mTools.getDevice();

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
            getString(R.string.project_website_uri) + "api/1/message/?version_code=" + projectVersionCode
                    + "&device=" + device,
            new Response.Listener<JSONObject>() {
                @SuppressLint("InlinedApi")
                @Override/*from  w  w  w.java  2  s  .c om*/
                public void onResponse(JSONObject response) {
                    try {
                        final long id = response.getLong("id");
                        final String title = response.getString("title");
                        final String message = response.getString("message");
                        final String bigMessage = response.getString("big_message");
                        final String button = response.getString("button");
                        final String uri = response.getString("uri");

                        final long lastId = mTools.getSharedPreferencesLong("MESSAGE_LAST_ID");

                        mTools.setSharedPreferencesLong("MESSAGE_LAST_ID", id);

                        if (lastId != 0 && id != lastId) {
                            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                                    R.drawable.ic_launcher);

                            NotificationManagerCompat notificationManager = NotificationManagerCompat
                                    .from(mContext);
                            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                                    mContext);

                            notificationBuilder.setWhen(mTools.getCurrentTime()).setAutoCancel(true)
                                    .setPriority(Notification.PRIORITY_HIGH)
                                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                                    .setCategory(Notification.CATEGORY_MESSAGE).setLargeIcon(bitmap)
                                    .setSmallIcon(R.drawable.ic_local_hospital_white_24dp)
                                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                    .setLights(Color.BLUE, 1000, 2000).setTicker(message).setContentTitle(title)
                                    .setContentText(message)
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(bigMessage));

                            if (!uri.equals("")) {
                                Intent launchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                                PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0,
                                        launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                                notificationBuilder.setContentIntent(launchPendingIntent).addAction(
                                        R.drawable.ic_local_hospital_white_24dp, button, launchPendingIntent);
                            }

                            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
                        }
                    } catch (Exception e) {
                        Log.e("MessageIntentService", Log.getStackTraceString(e));
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("MessageIntentService", error.toString());
                }
            });

    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    requestQueue.add(jsonObjectRequest);
}

From source file:com.jiee.smartplug.services.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received./*from   w w  w . j a  v  a2s  .c  o m*/
 */
private void sendNotification(String message) {

    System.out.println(message);

    NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = new Notification(R.drawable.svc_0_small_off, "title", System.currentTimeMillis());
    PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

    notify.setLatestEventInfo(getApplicationContext(), "Subject", message, pending);
    notif.notify(0, notify);

    //        Intent intent = new Intent(this, MainActivity.class);
    //        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    //                PendingIntent.FLAG_ONE_SHOT);
    //
    //        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
    //                .setSmallIcon(R.drawable.ic_stat_ic_notification)
    //                .setContentTitle("GCM Message")
    //                .setContentText(message)
    //                .setAutoCancel(true)
    //                .setSound(defaultSoundUri)
    //                .setContentIntent(pendingIntent);
    //
    //        NotificationManager notificationManager =
    //                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //
    //        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:com.elkriefy.android.apps.chubbytabby.activity.MainActivity.java

private PendingIntent createPendingShareIntent() {
    Intent actionIntent = new Intent(Intent.ACTION_SEND);
    actionIntent.setType("text/html");
    actionIntent.putExtra(Intent.EXTRA_TEXT, "This is the Chubby Bunny Challenge/n" + URL);
    return PendingIntent.getActivity(getApplicationContext(), 0, actionIntent, 0);
}

From source file:net.bible.android.device.ProgressNotificationManager.java

/** find the Progress object in our map to the associated Notifications
 * /*ww  w  .j a  v  a  2s.c  o m*/
 * @param prog
 * @return
 */
private Notification findOrCreateNotification(Progress prog) {
    Notification notification = progressMap.get(prog);
    if (notification == null) {
        Log.d(TAG, "Creating Notification for progress Hash:" + prog.hashCode());
        // configure the intent
        Intent intent = new Intent(BibleApplication.getApplication(), ProgressStatus.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(BibleApplication.getApplication(), 0,
                intent, 0);

        notification = new Notification(R.drawable.bible, prog.getJobName(), System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_AUTO_CANCEL;
        notification.contentView = new RemoteViews(SharedConstants.PACKAGE_NAME,
                R.layout.progress_notification);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.bible);
        notification.contentView.setTextViewText(R.id.status_text, "");
        notification.contentView.setProgressBar(R.id.status_progress, 100, prog.getWork(), false);

        progressMap.put(prog, notification);

        androidNotificationManager.notify(prog.hashCode(), notification);
    }

    return notification;
}