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:eu.faircode.adblocker.ServiceSinkhole.java

private void showAutoStartNotification() {
    Intent main = new Intent(this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_APPROVE, true);
    PendingIntent pi = PendingIntent.getActivity(this, NOTIFY_AUTOSTART, main,
            PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_error_white_24dp).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_autostart)).setContentIntent(pi).setColor(tv.data)
            .setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }/* ww w.  j  ava 2  s .c om*/

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_autostart));

    NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build());
}

From source file:jmri.enginedriver.threaded_application.java

/**
 * Display OnGoing Notification that indicates EngineDriver is Running.
 * Should only be called when ED is going into the background.
 * Currently call this from each activity onPause, passing the current intent 
 * to return to when reopening.  */
void addNotification(Intent notificationIntent) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon)
            .setContentTitle(this.getString(R.string.notification_title))
            .setContentText(this.getString(R.string.notification_text)).setOngoing(true);

    PendingIntent contentIntent = PendingIntent.getActivity(this, ED_NOTIFICATION_ID, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(ED_NOTIFICATION_ID, builder.build());
}

From source file:me.spadival.podmode.PodModeService.java

/** Updates the notification. */
void updateNotification(String text) {
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    mNotification = new Notification.Builder(getApplicationContext()).setContentTitle("PodMode")
            .setContentText(text).setContentIntent(pi).setSmallIcon(R.drawable.ic_stat_podmode)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true)
            .getNotification();//from   www .  j a v  a  2s  . com

    mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showExitNotification(String reason) {
    Intent main = new Intent(this, ActivityMain.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_error_white_24dp).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_error)).setContentIntent(pi).setColor(tv.data)
            .setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }/* www . j ava  2  s  .c om*/

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_error));
    notification.setSummaryText(reason);

    NotificationManagerCompat.from(this).notify(NOTIFY_EXIT, notification.build());
}

From source file:co.beem.project.beem.FbTextService.java

private PendingIntent makeChatIntent(User user) {
    Intent chatIntent = new Intent(FbTextService.this, FbTextMainActivity.class);
    chatIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
    try {//from w w  w. jav a2  s.  co  m
        chatIntent.setData(user.toUri());
    } catch (Exception e) {
        if (FbTextApplication.isDebug)
            Log.e(TAG, e.getMessage());
    }
    PendingIntent contentIntent = PendingIntent.getActivity(FbTextService.this, 0, chatIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return contentIntent;
}

From source file:me.spadival.podmode.PodModeService.java

/**
 * Configures service as a foreground service. A foreground service is a
 * service that's doing something the user is actively aware of (such as
 * playing music), and must appear to the user as a notification. That's why
 * we create the notification here.//from  ww w.ja va2s . c  o  m
 */
void setUpAsForeground(String text) {
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    mNotification = new Notification.Builder(getApplicationContext()).setContentTitle("PodMode")
            .setContentText(text).setContentIntent(pi).setSmallIcon(R.drawable.ic_stat_podmode)
            .setContentIntent(pi)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true)
            .getNotification();

    startForeground(NOTIFICATION_ID, mNotification);
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showErrorNotification(int error, String message) {
    Intent main = new Intent(this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_LOGCAT, true);
    PendingIntent pi = PendingIntent.getActivity(this, NOTIFY_ERROR, main, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_error_white_24dp).setContentTitle(getString(R.string.app_name))
            .setContentText(message).setContentIntent(pi).setNumber(error).setColor(tv.data).setOngoing(false)
            .setAutoCancel(true);//from  w  w  w . j a  v a 2  s.c  om

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(message);

    NotificationManagerCompat.from(this).notify(error + 100, notification.build());
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showAccessNotification(int uid) {
    String name = TextUtils.join(", ", Util.getApplicationNames(uid, ServiceSinkhole.this));

    Intent main = new Intent(ServiceSinkhole.this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
    PendingIntent pi = PendingIntent.getActivity(ServiceSinkhole.this, uid + 10000, main,
            PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOn, tv, true);
    int colorOn = tv.data;
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    int colorOff = tv.data;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_cloud_upload_white_24dp).setGroup("AccessAttempt")
            .setContentTitle(getString(R.string.app_name)).setContentText(getString(R.string.msg_access, name))
            .setContentIntent(pi).setColor(colorOff).setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }//from   w  w w  .  ja  va 2 s. co  m

    DateFormat df = new SimpleDateFormat("dd HH:mm");

    NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
    String sname = getString(R.string.msg_access, name);
    int pos = sname.indexOf(name);
    Spannable sp = new SpannableString(sname);
    sp.setSpan(new StyleSpan(Typeface.BOLD), pos, pos + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    notification.addLine(sp);

    Cursor cursor = DatabaseHelper.getInstance(ServiceSinkhole.this).getAccessUnset(uid, 7);
    int colDAddr = cursor.getColumnIndex("daddr");
    int colTime = cursor.getColumnIndex("time");
    int colAllowed = cursor.getColumnIndex("allowed");
    while (cursor.moveToNext()) {
        StringBuilder sb = new StringBuilder();
        sb.append(df.format(cursor.getLong(colTime))).append(' ');

        String daddr = cursor.getString(colDAddr);
        if (Util.isNumericAddress(daddr))
            try {
                daddr = InetAddress.getByName(daddr).getHostName();
            } catch (UnknownHostException ignored) {
            }
        sb.append(daddr);

        int allowed = cursor.getInt(colAllowed);
        if (allowed >= 0) {
            pos = sb.indexOf(daddr);
            sp = new SpannableString(sb);
            ForegroundColorSpan fgsp = new ForegroundColorSpan(allowed > 0 ? colorOn : colorOff);
            sp.setSpan(fgsp, pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        notification.addLine(sp);
    }
    cursor.close();

    NotificationManagerCompat.from(this).notify(uid + 10000, notification.build());
}

From source file:eu.faircode.netguard.ServiceSinkhole.java

public void notifyNewApplication(int uid) {
    if (uid < 0)
        return;// w w  w  .  ja va 2  s  .  c o m

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        // Get application name
        String name = TextUtils.join(", ", Util.getApplicationNames(uid, this));

        // Get application info
        PackageManager pm = getPackageManager();
        String[] packages = pm.getPackagesForUid(uid);
        if (packages == null || packages.length < 1)
            throw new PackageManager.NameNotFoundException(Integer.toString(uid));
        boolean internet = Util.hasInternet(uid, this);

        // Build notification
        Intent main = new Intent(this, ActivityMain.class);
        main.putExtra(ActivityMain.EXTRA_REFRESH, true);
        main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
        PendingIntent pi = PendingIntent.getActivity(this, uid, main, PendingIntent.FLAG_UPDATE_CURRENT);

        TypedValue tv = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_security_white_24dp).setContentIntent(pi).setColor(tv.data)
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            builder.setContentTitle(name).setContentText(getString(R.string.msg_installed_n));
        else
            builder.setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.msg_installed, name));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);

        // Get defaults
        SharedPreferences prefs_wifi = getSharedPreferences("wifi", Context.MODE_PRIVATE);
        SharedPreferences prefs_other = getSharedPreferences("other", Context.MODE_PRIVATE);
        boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true));
        boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true));

        // Build Wi-Fi action
        Intent riWifi = new Intent(this, ServiceSinkhole.class);
        riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi");
        riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi);

        PendingIntent piWifi = PendingIntent.getService(this, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action wAction = new NotificationCompat.Action.Builder(
                wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
                getString(wifi ? R.string.title_allow_wifi : R.string.title_block_wifi), piWifi).build();
        builder.addAction(wAction);

        // Build mobile action
        Intent riOther = new Intent(this, ServiceSinkhole.class);
        riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other");
        riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other);
        PendingIntent piOther = PendingIntent.getService(this, uid + 10000, riOther,
                PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action oAction = new NotificationCompat.Action.Builder(
                other ? R.drawable.other_on : R.drawable.other_off,
                getString(other ? R.string.title_allow_other : R.string.title_block_other), piOther).build();
        builder.addAction(oAction);

        // Show notification
        if (internet)
            NotificationManagerCompat.from(this).notify(uid, builder.build());
        else {
            NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                expanded.bigText(getString(R.string.msg_installed_n));
            else
                expanded.bigText(getString(R.string.msg_installed, name));
            expanded.setSummaryText(getString(R.string.title_internet));
            NotificationManagerCompat.from(this).notify(uid, expanded.build());
        }

    } catch (PackageManager.NameNotFoundException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showUpdateNotification(String name, String url) {
    Intent download = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    PendingIntent pi = PendingIntent.getActivity(this, 0, download, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_security_white_24dp).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_update)).setContentIntent(pi).setColor(tv.data)
            .setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }/*from   w  w w  .j  a va  2  s.  c  om*/

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_update));
    notification.setSummaryText(name);

    NotificationManagerCompat.from(this).notify(NOTIFY_UPDATE, notification.build());
}