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.android.launcher3.widget.DigitalAppWidgetProvider.java

private void updateClock(Context context, AppWidgetManager appWidgetManager, int appWidgetId, float ratio) {
    RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);

    // Launch setinngs when clicking on the time in the widget only if not a lock screen widget
    Bundle newOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
    if (newOptions != null && newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
            -1) != AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
        Intent mIntent = new Intent(Intent.ACTION_MAIN);
        mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName component = new ComponentName("com.android.settings", "com.android.settings.Settings");
        mIntent.setComponent(component);
        widget.setOnClickPendingIntent(R.id.digital_appwidget,
                PendingIntent.getActivity(context, 0, mIntent, 0));
    }//from ww w  . ja  v a  2  s.  c om

    //cg sai.pan begin
    refreshWifiStatus(context, widget);
    refreshBtStatus(context, widget);
    refreshAirplaneStatus(context, widget,
            Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0);
    mLocationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    refreshGpsStatus(context, widget, mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
    //refreshDataStatus(context, widget, Settings.Global.getInt(context.getContentResolver(), Settings.Global.MOBILE_DATA, 0) != 0);
    requestLocation(context);
    //cg sai.pan end

    // SPRD for bug421127 add am/pm for widget
    WidgetUtils.setTimeFormat(widget, (int) context.getResources().getDimension(R.dimen.widget_label_font_size),
            R.id.the_clock);
    WidgetUtils.setClockSize(context, widget, ratio);

    // Set today's date format
    CharSequence dateFormat = DateFormat.getBestDateTimePattern(Locale.getDefault(),
            context.getString(R.string.abbrev_wday_month_day_no_year));
    widget.setCharSequence(R.id.date, "setFormat12Hour", dateFormat);
    widget.setCharSequence(R.id.date, "setFormat24Hour", dateFormat);

    appWidgetManager.updateAppWidget(appWidgetId, widget);
}

From source file:dk.microting.softkeyboard.autoupdateapk.AutoUpdateApk.java

private void raise_notification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    String update_file = preferences.getString(UPDATE_FILE, "");
    if (update_file.length() > 0) {
        // raise notification
        Notification notification = new Notification(appIcon, appName + " update", System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://" + context.getFilesDir().getAbsolutePath() + "/" + update_file),
                ANDROID_PACKAGE);/*from   w w w.java  2s.  co m*/
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        nm.notify(NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }
}

From source file:by.zatta.pilight.connection.ConnectionService.java

private static void makeNotification(NotificationType type, String message) {
    Intent main;//from  w w w  . j  a  va  2 s  . c o  m
    Intent kill;
    PendingIntent sentBroadcast;
    PendingIntent startMainActivity;
    PendingIntent killService;

    String myDate = java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());

    //Log.v(TAG, "setting notification: " + type.name() + " while current = " + mCurrentNotif.name());
    if (type != mCurrentNotif) {
        //Log.v(TAG, "setting NEW notification: " + type.name());
        sendMessageToUI(MSG_SET_STATUS, type.name());
        switch (type) {
        case DESTROYED:
            builder = new Notification.Builder(ctx);
            builder.setSmallIcon(R.drawable.eye_black).setLargeIcon(bigPic(R.drawable.eye_black))
                    .setContentTitle(aCtx.getString(R.string.app_name)).setContentText(message);
            mCurrentNotif = NotificationType.DESTROYED;
            break;
        case CONNECTING:
            kill = new Intent("pilight-kill-service");
            killService = PendingIntent.getBroadcast(ctx, 0, kill, PendingIntent.FLAG_UPDATE_CURRENT);

            builder = new Notification.Builder(ctx).setContentTitle(aCtx.getString(R.string.app_name))
                    .setContentText(message).setDeleteIntent(killService).setSmallIcon(R.drawable.eye_trans)
                    .setLargeIcon(bigPic(R.drawable.eye_trans));
            mCurrentNotif = NotificationType.CONNECTING;
            break;
        case CONNECTED:
            main = new Intent(ctx, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startMainActivity = PendingIntent.getActivity(ctx, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);

            builder = new Notification.Builder(ctx).setContentTitle(aCtx.getString(R.string.app_name))
                    .setContentText(message + "\n" + myDate).setContentIntent(startMainActivity)
                    .setSmallIcon(R.drawable.eye_white).setLargeIcon(bigPic(R.drawable.eye_white));
            mCurrentNotif = NotificationType.CONNECTED;
            break;
        case FAILED:
            main = new Intent("pilight-reconnect");
            sentBroadcast = PendingIntent.getBroadcast(ctx, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
            kill = new Intent("pilight-kill-service");
            killService = PendingIntent.getBroadcast(ctx, 0, kill, PendingIntent.FLAG_UPDATE_CURRENT);

            builder = new Notification.Builder(ctx).setContentTitle("pilight")
                    .setContentText(aCtx.getString(R.string.noti_failed)).setDeleteIntent(killService)
                    .addAction(R.drawable.action_refresh, aCtx.getString(R.string.noti_retry), sentBroadcast)
                    .setSmallIcon(R.drawable.eye_trans).setLargeIcon(bigPic(R.drawable.eye_trans));
            mCurrentNotif = NotificationType.FAILED;
            break;
        case LOST_CONNECTION:
            main = new Intent("pilight-reconnect");
            sentBroadcast = PendingIntent.getBroadcast(ctx, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
            kill = new Intent("pilight-kill-service");
            killService = PendingIntent.getBroadcast(ctx, 0, kill, PendingIntent.FLAG_UPDATE_CURRENT);

            builder = new Notification.Builder(ctx).setContentTitle(aCtx.getString(R.string.app_name))
                    .setContentText(message + "\n" + myDate).setDeleteIntent(killService)
                    .addAction(R.drawable.action_refresh, aCtx.getString(R.string.noti_retry), sentBroadcast)
                    .setSmallIcon(R.drawable.eye_trans).setLargeIcon(bigPic(R.drawable.eye_trans));
            mCurrentNotif = NotificationType.LOST_CONNECTION;
            break;
        case UPDATE:
            main = new Intent(ctx, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startMainActivity = PendingIntent.getActivity(ctx, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
            String[] title = message.split("\n");
            message = message.replace(title[0] + "\n", "");
            builder = new Notification.Builder(ctx).setContentTitle(title[0]).setContentText(message)
                    .setStyle(new Notification.BigTextStyle().bigText(message))
                    .setContentIntent(startMainActivity).setSmallIcon(R.drawable.eye_white)
                    .setLargeIcon(bigPic(R.drawable.eye_white));
            mCurrentNotif = NotificationType.UPDATE;
            break;
        default:
            break;
        }
    } else {
        if (message != null) {
            if (message.contains("Stamp")) {
                String[] title = message.split("\n");
                message = message.replace(title[0] + "\n", "");
                builder.setContentTitle(title[0]).setStyle(new Notification.BigTextStyle().bigText(message));
                builder.setContentText(message);
            } else {
                builder.setContentTitle(myDate).setStyle(new Notification.BigTextStyle().bigText(message));
                builder.setContentText(message);
            }
        }
    }
    mNotMan.notify(35, builder.build());
}

From source file:com.android.settings.locationprivacy.LocationPrivacyDialog.java

@Override
protected void onPause() {
    System.out.println("onPause");
    if (needRestartActivity) {
        Intent intent = new Intent(this, LocationPrivacyDialog.class);
        intent.putExtra("app", app);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification noti = new Notification.Builder(this)
                .setContentTitle(getResources().getString(R.string.lp_notification_title))
                .setContentText(getResources().getString(R.string.lp_notification_))
                .setSmallIcon(R.drawable.ic_settings_locationprivacy).setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, noti);

    }//from  www .ja  va 2s.c  o m
    super.onPause();
}

From source file:am.roadpolice.roadpolice.alarm.AlarmReceiver.java

@Override
public void submissionCompleted(ArrayList<ViolationInfo> list, String result) {
    // If errors occurs while trying to update data.
    if (!TextUtils.isEmpty(result)) {
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor ed = sp.edit();
        ed.putBoolean(MainActivity.MISS_UPDATE_DUE_TO_NETWORK_ISSUE, true);
        ed.apply();/*from  w w w . j  ava2s .c om*/
        return;
    }

    SQLiteHelper dbHelper = new SQLiteHelper(mContext);
    // Get list of new violation info, if null no new items found.
    ArrayList<ViolationInfo> newViolationInfoList = dbHelper.insertViolationInfoListAndCheckNew(list);

    // Create notification.
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
    // Create notification Title Text.
    int updateInterval = DialogSettings.getUpdateInterval(mContext);
    switch (updateInterval) {
    case DialogSettings.DAILY:
        mBuilder.setContentTitle(mContext.getString(R.string.txtDailyNotification));
        break;

    case DialogSettings.WEEKLY:
        mBuilder.setContentTitle(mContext.getString(R.string.txtWeeklyNotification));
        break;

    case DialogSettings.MONTHLY:
        mBuilder.setContentTitle(mContext.getString(R.string.txtMonthlyNotification));
        break;
    }

    // Create notification message.
    if (newViolationInfoList == null || newViolationInfoList.size() < 1) {
        mBuilder.setContentText(mContext.getString(R.string.txtNoViolationNotification));
        mBuilder.setSmallIcon(R.drawable.ic_checked);
    } else {
        mBuilder.setContentText(mContext.getString(R.string.txtYesViolationNotification));
        mBuilder.setSmallIcon(R.drawable.ic_attention);
    }

    // Activate vibration and sound.
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    mBuilder.setAutoCancel(true);

    // Set intent to open Main Activity, when user click on notification.
    Intent notificationIntent = new Intent(mContext, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent mainActivityPendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

    mBuilder.setContentIntent(mainActivityPendingIntent);

    // Show notification.
    NotificationManager mNotificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());

    // Get shared preferences.
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
    SharedPreferences.Editor ed = sp.edit();
    ed.putBoolean(MainActivity.MISS_UPDATE_DUE_TO_NETWORK_ISSUE, false);
    ed.apply();

}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void showIncompatibleVersionNotification(Context context) {
    Intent intent = new Intent(context, UpdateRequiredActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
    Notification notification = new Notification.Builder(context)
            .setContentTitle(context.getText(R.string.cmaccount_update_required_title))
            .setContentText(context.getText(R.string.cmaccount_update_required_short))
            .setSmallIcon(R.drawable.ic_dialog_alert)
            .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.icon)).getBitmap())
            .setContentIntent(contentIntent).build();
    CMAccountUtils.showNotification(context, CMAccount.NOTIFICATION_ID_INCOMPATIBLE_VERSION, notification);
}

From source file:pt.up.mobile.syncadapter.SigarraSyncAdapter.java

private void syncNotifications(Account account, SyncResult syncResult)
        throws AuthenticationException, IOException {
    final User user = AccountUtils.getUser(getContext(), account.name);
    final String notificationReply = SifeupAPI.getReply(SifeupAPI.getNotificationsUrl(user.getUserCode()),
            account, getContext());// ww w  . j av a 2s . c o  m
    final Gson gson = GsonUtils.getGson();
    final Notification[] notifications = gson.fromJson(notificationReply, Notification[].class);
    if (notifications == null) {
        syncResult.stats.numParseExceptions++;
        LogUtils.trackException(getContext(), new RuntimeException(), notificationReply, true);
        return;
    }
    ArrayList<String> fetchedNotCodes = new ArrayList<String>();
    ArrayList<ContentValues> bulkValues = new ArrayList<ContentValues>();
    for (Notification not : notifications) {
        final ContentValues values = new ContentValues();
        values.put(SigarraContract.Notifcations.CONTENT, gson.toJson(not));
        fetchedNotCodes.add(not.getCode());
        if (getContext().getContentResolver().update(SigarraContract.Notifcations.CONTENT_URI, values,
                SigarraContract.Notifcations.UPDATE_NOTIFICATION,
                SigarraContract.Notifcations.getNotificationsSelectionArgs(account.name, not.getCode())) == 0) {
            values.put(SigarraContract.Notifcations.CODE, account.name);
            values.put(SigarraContract.Notifcations.ID_NOTIFICATION, not.getCode());
            values.put(SigarraContract.Notifcations.STATE, SigarraContract.Notifcations.NEW);
            values.put(SigarraContract.Notifcations.CODE, account.name);
            bulkValues.add(values);
        }

    }
    // inserting the values
    if (bulkValues.size() > 0) {
        getContext().getContentResolver().bulkInsert(SigarraContract.Notifcations.CONTENT_URI,
                bulkValues.toArray(new ContentValues[0]));
        // if the account being synced is the current active accout
        // display notification
        if (AccountUtils.getActiveUserName(getContext()).equals(account.name)) {
            final NotificationManager mNotificationManager = (NotificationManager) getContext()
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(getContext());
            notBuilder.setAutoCancel(true).setOnlyAlertOnce(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            if (bulkValues.size() == 1) {
                final Notification notification = gson.fromJson(
                        bulkValues.get(0).getAsString(SigarraContract.Notifcations.CONTENT),
                        Notification.class);
                Intent notifyIntent = new Intent(getContext(), NotificationsDescActivity.class)
                        .putExtra(NotificationsDescFragment.NOTIFICATION, notification);
                // Creates the PendingIntent
                PendingIntent notifyPendingIntent = PendingIntent.getActivity(getContext(), 0, notifyIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                // Sets the Activity to start in a new, empty task
                notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                notBuilder.setSmallIcon(R.drawable.icon).setTicker(notification.getMessage())
                        .setContentTitle(notification.getSubject()).setContentText(notification.getMessage())
                        .setContentIntent(notifyPendingIntent)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getMessage())
                                .setBigContentTitle(notification.getSubject())
                                .setSummaryText(notification.getMessage()));
                mNotificationManager.notify(notification.getCode().hashCode(), notBuilder.build());
            } else {
                final String notTitle = getContext().getString(R.string.new_notifications, bulkValues.size());

                Intent notifyIntent = new Intent(getContext(), NotificationsActivity.class);
                // Sets the Activity to start in a new, empty task
                notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                // Creates the PendingIntent
                PendingIntent notifyPendingIntent = PendingIntent.getActivity(getContext(), 0, notifyIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
                // Sets a title for the Inbox style big view
                inboxStyle.setBigContentTitle(notTitle);
                // Moves events into the big view
                for (ContentValues value : bulkValues) {
                    final Notification notification = gson.fromJson(
                            value.getAsString(SigarraContract.Notifcations.CONTENT), Notification.class);
                    inboxStyle.addLine(notification.getSubject());
                }
                // Moves the big view style object into the notification
                // object.
                notBuilder.setStyle(inboxStyle);
                notBuilder.setSmallIcon(R.drawable.icon).setTicker(notTitle).setContentTitle(notTitle)
                        .setContentText("").setContentIntent(notifyPendingIntent);
                mNotificationManager.notify(NotificationsFragment.class.getName().hashCode(),
                        notBuilder.build());
            }

        }
    }
    final Cursor syncState = getContext().getContentResolver().query(SigarraContract.LastSync.CONTENT_URI,
            SigarraContract.LastSync.COLUMNS, SigarraContract.LastSync.PROFILE,
            SigarraContract.LastSync.getLastSyncSelectionArgs(AccountUtils.getActiveUserName(getContext())),
            null);
    try {
        if (syncState.moveToFirst()) {
            if (syncState.getLong(syncState.getColumnIndex(SigarraContract.LastSync.NOTIFICATIONS)) == 0) {
                // Report that we have checked the notifications
                final ContentValues values = new ContentValues();
                values.put(SigarraContract.LastSync.NOTIFICATIONS, System.currentTimeMillis());
                getContext().getContentResolver().update(SigarraContract.LastSync.CONTENT_URI, values,
                        SigarraContract.LastSync.PROFILE,
                        SigarraContract.LastSync.getLastSyncSelectionArgs(account.name));
            }
        }
    } finally {
        syncState.close();
    }
    ArrayList<String> notToDelete = new ArrayList<String>();
    final Cursor cursor = getContext().getContentResolver().query(SigarraContract.Notifcations.CONTENT_URI,
            new String[] { SigarraContract.Notifcations.ID_NOTIFICATION }, SigarraContract.Notifcations.PROFILE,
            SigarraContract.Notifcations.getNotificationsSelectionArgs(account.name), null);
    try {
        if (cursor.moveToFirst()) {
            do {
                final String code = cursor.getString(0);
                if (!fetchedNotCodes.contains(code))
                    notToDelete.add(code);
            } while (cursor.moveToNext());
        } else {
            // no notifications
            getContext().getContentResolver().notifyChange(SigarraContract.Notifcations.CONTENT_URI, null);
        }
    } finally {
        cursor.close();
    }
    if (notToDelete.size() > 0)
        getContext().getContentResolver().delete(SigarraContract.Notifcations.CONTENT_URI,
                SigarraContract.Notifcations.getNotificationsDelete(notToDelete.toArray(new String[0])),
                SigarraContract.Notifcations.getNotificationsSelectionArgs(account.name,
                        notToDelete.toArray(new String[0])));
    syncResult.stats.numEntries += notifications.length;
}

From source file:com.phonemetra.account.util.AccountUtils.java

public static void showIncompatibleVersionNotification(Context context) {
    Intent intent = new Intent(context, UpdateRequiredActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
    Notification notification = new Notification.Builder(context)
            .setContentTitle(context.getText(R.string.Account_update_required_title))
            .setContentText(context.getText(R.string.Account_update_required_short))
            .setSmallIcon(R.drawable.ic_dialog_alert)
            .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.icon)).getBitmap())
            .setContentIntent(contentIntent).build();
    AccountUtils.showNotification(context, Account.NOTIFICATION_ID_INCOMPATIBLE_VERSION, notification);
}

From source file:com.fanfou.app.opensource.service.DownloadService.java

private void showProgress() {
    this.notification = new Notification(R.drawable.ic_notify_download, "?",
            System.currentTimeMillis());
    this.notification.flags |= Notification.FLAG_ONGOING_EVENT;
    this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
    this.notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    final RemoteViews view = new RemoteViews(getPackageName(), R.layout.download_notification);
    view.setTextViewText(R.id.download_notification_text, "? 0%");
    view.setProgressBar(R.id.download_notification_progress, 100, 0, false);
    this.notification.contentView = view;
    this.nm.notify(DownloadService.NOTIFICATION_PROGRESS_ID, this.notification);
}

From source file:name.setup.dance.StepService.java

/**
 * Show a notification while this service is running.
 *//*from   w w w  .ja v a  2  s  .co  m*/
private void showNotification() {
    CharSequence text = getText(R.string.app_name);
    Notification notification = new Notification(R.drawable.ic_notification, null, System.currentTimeMillis());
    notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    Intent DanceStepAppIntent = new Intent();
    DanceStepAppIntent.setComponent(new ComponentName(this, DanceStepApp.class));
    DanceStepAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, DanceStepAppIntent, 0);
    notification.setLatestEventInfo(this, text, getText(R.string.notification_subtitle), contentIntent);

    mNM.notify(R.string.app_name, notification);
}