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.arubadesignweb.app.appoverview.util.GcmIntentService.java

private void sendNotificationSuccess(Bundle extras) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

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

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setDefaults(Notification.DEFAULT_ALL).setSmallIcon(R.drawable.push_icon)
            .setContentTitle(extras.getString("title"))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(extras.getString("message")))
            .setContentText(extras.getString("message"));

    mBuilder.setAutoCancel(true);/*from   w  ww.j  a  v  a 2s  . co m*/
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    CommonUtilities.markAsDisplayed(extras.getString("message_id"));
}

From source file:com.arubadesignweb.app.appoverview.util.GcmIntentService.java

public static void sendLocalNotification(Context context, String title, String message, String message_id) {
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Application.class),
            0);/*w w w  . j  a v  a2  s.  c o m*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setDefaults(Notification.DEFAULT_ALL).setSmallIcon(R.drawable.push_icon).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message)).setContentText(message);

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

    CommonUtilities.markAsDisplayed(message_id);
}

From source file:at.vcity.androidim.services.IMService.java

/**
 * Show a notification while this service is running.
 * @param msg /*from   w  w  w.jav  a2  s  . c om*/
 **/
private void showNotification(String username, String msg) {
    // Set the icon, scrolling text and TIMESTAMP
    String title = "AndroidIM: You got a new Message! (" + username + ")";

    String text = username + ": " + ((msg.length() < 5) ? msg : msg.substring(0, 5) + "...");

    //NotificationCompat.Builder notification = new NotificationCompat.Builder(R.drawable.stat_sample, title,System.currentTimeMillis());
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.stat_sample).setContentTitle(title).setContentText(text);

    Intent i = new Intent(this, Messaging.class);
    i.putExtra(FriendInfo.USERNAME, username);
    i.putExtra(MessageInfo.MESSAGETEXT, msg);

    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);

    // Set the info for the views that show in the notification panel.
    // msg.length()>15 ? MSG : msg.substring(0, 15);
    mBuilder.setContentIntent(contentIntent);

    mBuilder.setContentText("New message from " + username + ": " + msg);

    //TODO: it can be improved, for instance message coming from same user may be concatenated 
    // next version

    // Send the notification.
    // We use a layout id because it is a unique number.  We use it later to cancel.
    mNM.notify((username + msg).hashCode(), mBuilder.build());
}

From source file:luan.com.flippit.GcmIntentService.java

private void textNotification(String msg) {
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    Log.i(MyActivity.TAG, getClass().getName() + ": " + "Text message.");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.action_icon).setContentTitle("FlippIt")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setTicker("Copied to clipboard")
            .setContentText(msg);// w w w .ja  va2s. co  m

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.cancel(1);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    new CopyClipboard(msg, mContext);
    UpdateHistory updateHistory = new UpdateHistory();
    updateHistory.updateHistory(mContext);
}

From source file:net.olejon.spotcommander.WebViewActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Google API client
    mGoogleApiClient = new GoogleApiClient.Builder(mContext).addApiIfAvailable(Wearable.API).build();

    // Allow landscape?
    if (!mTools.allowLandscape())
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // Hide status bar?
    if (mTools.getDefaultSharedPreferencesBoolean("HIDE_STATUS_BAR"))
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Power manager
    final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

    //noinspection deprecation
    mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "wakeLock");

    // Settings/*  w ww .  j  av  a  2  s .  c o  m*/
    mTools.setSharedPreferencesBoolean("CAN_CLOSE_COVER", false);

    // Current network
    mCurrentNetwork = mTools.getCurrentNetwork();

    // Computer
    final long computerId = mTools.getSharedPreferencesLong("LAST_COMPUTER_ID");

    final String[] computer = mTools.getComputer(computerId);

    final String uri = computer[0];
    final String username = computer[1];
    final String password = computer[2];

    // Layout
    setContentView(R.layout.activity_webview);

    // Status bar color
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mStatusBarPrimaryColor = getWindow().getStatusBarColor();
        mStatusBarCoverArtColor = mStatusBarPrimaryColor;
    }

    // Notification
    mPersistentNotificationIsSupported = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN);

    if (mPersistentNotificationIsSupported) {
        final Intent launchActivityIntent = new Intent(mContext, MainActivity.class);
        launchActivityIntent.setAction("android.intent.action.MAIN");
        launchActivityIntent.addCategory("android.intent.category.LAUNCHER");
        mLaunchActivityPendingIntent = PendingIntent.getActivity(mContext, 0, launchActivityIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent hideIntent = new Intent(mContext, RemoteControlIntentService.class);
        hideIntent.setAction("hide_notification");
        hideIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mHidePendingIntent = PendingIntent.getService(mContext, 0, hideIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent previousIntent = new Intent(mContext, RemoteControlIntentService.class);
        previousIntent.setAction("previous");
        previousIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mPreviousPendingIntent = PendingIntent.getService(mContext, 0, previousIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent playPauseIntent = new Intent(mContext, RemoteControlIntentService.class);
        playPauseIntent.setAction("play_pause");
        playPauseIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mPlayPausePendingIntent = PendingIntent.getService(mContext, 0, playPauseIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent nextIntent = new Intent(mContext, RemoteControlIntentService.class);
        nextIntent.setAction("next");
        nextIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mNextPendingIntent = PendingIntent.getService(mContext, 0, nextIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent volumeDownIntent = new Intent(mContext, RemoteControlIntentService.class);
        volumeDownIntent.setAction("adjust_spotify_volume_down");
        volumeDownIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mVolumeDownPendingIntent = PendingIntent.getService(mContext, 0, volumeDownIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        final Intent volumeUpIntent = new Intent(mContext, RemoteControlIntentService.class);
        volumeUpIntent.setAction("adjust_spotify_volume_up");
        volumeUpIntent.putExtra(RemoteControlIntentService.REMOTE_CONTROL_INTENT_SERVICE_EXTRA, computerId);
        mVolumeUpPendingIntent = PendingIntent.getService(mContext, 0, volumeUpIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        mNotificationManager = NotificationManagerCompat.from(mContext);
        mNotificationBuilder = new NotificationCompat.Builder(mContext);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            mNotificationBuilder.setPriority(Notification.PRIORITY_MAX);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mNotificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            mNotificationBuilder.setCategory(Notification.CATEGORY_TRANSPORT);
        }
    }

    // Web view
    mWebView = (WebView) findViewById(R.id.webview_webview);

    mWebView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.background));
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);

    mWebView.setWebViewClient(new WebViewClient() {
        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && !url.contains(uri)
                    && !url.contains("olejon.net/code/spotcommander/api/1/spotify/")
                    && !url.contains("accounts.spotify.com/") && !url.contains("facebook.com/")) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

                return true;
            }

            return false;
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view, @NonNull HttpAuthHandler handler, String host,
                String realm) {
            if (handler.useHttpAuthUsernamePassword()) {
                handler.proceed(username, password);
            } else {
                handler.cancel();

                mWebView.stopLoading();

                mTools.showToast(getString(R.string.webview_authentication_failed), 1);

                mTools.navigateUp(mActivity);
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest webResourceRequest,
                WebResourceError webResourceError) {
            mWebView.stopLoading();

            mTools.showToast(getString(R.string.webview_error), 1);

            mTools.navigateUp(mActivity);
        }

        @Override
        public void onReceivedSslError(WebView view, @NonNull SslErrorHandler handler, SslError error) {
            handler.cancel();

            mWebView.stopLoading();

            new MaterialDialog.Builder(mContext).title(R.string.webview_dialog_ssl_error_title)
                    .content(getString(R.string.webview_dialog_ssl_error_message))
                    .positiveText(R.string.webview_dialog_ssl_error_positive_button)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog materialDialog,
                                @NonNull DialogAction dialogAction) {
                            finish();
                        }
                    }).contentColorRes(R.color.black).show();
        }
    });

    // User agent
    mProjectVersionName = mTools.getProjectVersionName();

    final String uaAppend1 = (!username.equals("") && !password.equals("")) ? "AUTHENTICATION_ENABLED " : "";
    final String uaAppend2 = (mTools.getSharedPreferencesBoolean("WEAR_CONNECTED")) ? "WEAR_CONNECTED " : "";
    final String uaAppend3 = (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
            && !mTools.getDefaultSharedPreferencesBoolean("HARDWARE_ACCELERATED_ANIMATIONS"))
                    ? "DISABLE_CSSTRANSITIONS DISABLE_CSSTRANSFORMS3D "
                    : "";

    // Web settings
    final WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);
    webSettings.setUserAgentString(getString(R.string.webview_user_agent, webSettings.getUserAgentString(),
            mProjectVersionName, uaAppend1, uaAppend2, uaAppend3));

    // Load app
    if (savedInstanceState != null) {
        mWebView.restoreState(savedInstanceState);
    } else {
        mWebView.loadUrl(uri);
    }

    // JavaScript interface
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");
}

From source file:io.github.carlorodriguez.alarmon.AlarmClockService.java

private void refreshNotification() {
    String resolvedString = getString(R.string.no_pending_alarms);

    AlarmTime nextTime = pendingAlarms.nextAlarmTime();

    if (nextTime != null) {
        Map<String, String> values = new HashMap<>();

        values.put("t", nextTime.localizedString(getApplicationContext()));

        values.put("c", nextTime.timeUntilString(getApplicationContext()));

        String templateString = AppSettings.getNotificationTemplate(getApplicationContext());

        StrSubstitutor sub = new StrSubstitutor(values);

        resolvedString = sub.replace(templateString);
    }// w  w w  . j  a  v a2s .  c o  m

    // Make the notification launch the UI Activity when clicked.
    final Intent notificationIntent = new Intent(this, ActivityAlarmClock.class);
    final PendingIntent launch = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Context c = getApplicationContext();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

    String notificationTitle = getString(R.string.app_name);

    if (pendingAlarms.nextAlarmId() != AlarmClockServiceBinder.NO_ALARM_ID) {
        DbAccessor db = new DbAccessor(getApplicationContext());

        AlarmInfo alarmInfo = db.readAlarmInfo(pendingAlarms.nextAlarmId());

        if (alarmInfo != null) {
            notificationTitle = alarmInfo.getName() != null && !alarmInfo.getName().isEmpty()
                    ? alarmInfo.getName()
                    : getString(R.string.app_name);
        }

        db.closeConnections();
    }

    Notification notification = builder.setContentIntent(launch).setSmallIcon(R.drawable.ic_stat_notify_alarm)
            .setContentTitle(notificationTitle).setContentText(resolvedString)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.notification_color)).build();
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (pendingAlarms.size() > 0 && AppSettings.displayNotificationIcon(c)) {
        manager.notify(NOTIFICATION_BAR_ID, notification);
    } else {
        manager.cancel(NOTIFICATION_BAR_ID);
    }

    setSystemAlarmStringOnLockScreen(getApplicationContext(), nextTime);
}

From source file:foam.zizim.android.BoskoiService.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
    newBoskoiReportNotification = new Notification(R.drawable.favicon, tickerText, System.currentTimeMillis());
    newBoskoiReportNotification.contentIntent = contentIntent;
    newBoskoiReportNotification.flags = Notification.FLAG_AUTO_CANCEL;
    newBoskoiReportNotification.defaults = Notification.DEFAULT_ALL;
    newBoskoiReportNotification.setLatestEventInfo(this, TAG, tickerText, contentIntent);
    if (ringtone) {
        // set the ringer
        Uri ringURI = Uri.fromFile(new File("/system/media/audio/ringtones/ringer.mp3"));
        newBoskoiReportNotification.sound = ringURI;
    }/*from ww  w.  j  a  va 2  s.c  om*/

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

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

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

    mNotificationManager.notify(NOTIFICATION_ID, newBoskoiReportNotification);
}

From source file:be.vbsteven.bmtodesk.BackgroundSharingService.java

/**
 * shows notification when the sending failed
 *
 * @param message//from ww  w  . j  a va 2s.c  om
 */
private void showFailedSend(String message) {
    Notification n = new Notification(R.drawable.icon, "Sending bookmark failed", System.currentTimeMillis());
    n.flags = Notification.FLAG_AUTO_CANCEL;
    Intent i = new Intent(this, ShareActivity.class);
    i.putExtra(Global.EXTRA_TITLE, title);
    i.putExtra(Global.EXTRA_URL, url);
    PendingIntent p = PendingIntent.getActivity(this, 4, i, 0);
    n.setLatestEventInfo(this, "Sending bookmark failed", message, p);
    nManager.notify(3, n);
}