Esempio n. 1
0
  @Override
  protected void onHandleIntent(Intent intent) {
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this);

    builder
        .setContent(buildContent(0))
        .setTicker(getText(R.string.ticker), buildTicker())
        .setContentIntent(buildContentIntent())
        .setLargeIcon(buildLargeIcon())
        .setSmallIcon(R.drawable.ic_stat_notif_small_icon)
        .setOngoing(true);

    Notification notif = builder.getNotification();

    for (int i = 0; i < 20; i++) {
      notif.contentView.setProgressBar(android.R.id.progress, 100, i * 5, false);
      mgr.notify(NOTIFICATION_ID, notif);

      if (i == 0) {
        notif.tickerText = null;
        notif.tickerView = null;
      }

      SystemClock.sleep(1000);
    }

    mgr.cancel(NOTIFICATION_ID);
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    final boolean enabled = intent.getBooleanExtra(LocationManager.EXTRA_GPS_ENABLED, false);

    boolean visible;
    int iconId, textResId;

    if (action.equals(LocationManager.GPS_FIX_CHANGE_ACTION) && enabled) {
      // GPS is getting fixes
      iconId = com.android.internal.R.drawable.stat_sys_gps_on;
      textResId = R.string.gps_notification_found_text;
      visible = true;
    } else if (action.equals(LocationManager.GPS_ENABLED_CHANGE_ACTION) && !enabled) {
      // GPS is off
      visible = false;
      iconId = textResId = 0;
    } else {
      // GPS is on, but not receiving fixes
      iconId = R.drawable.stat_sys_gps_acquiring_anim;
      textResId = R.string.gps_notification_searching_text;
      visible = true;
    }

    try {
      if (visible) {
        Intent gpsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        gpsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, gpsIntent, 0);

        Notification n =
            new Notification.Builder(mContext)
                .setSmallIcon(iconId)
                .setContentTitle(mContext.getText(textResId))
                .setOngoing(true)
                .setContentIntent(pendingIntent)
                .getNotification();

        // Notification.Builder will helpfully fill these out for you no matter what you do
        n.tickerView = null;
        n.tickerText = null;

        n.priority = Notification.PRIORITY_HIGH;

        int[] idOut = new int[1];
        mNotificationService.enqueueNotificationWithTag(
            mContext.getPackageName(), null, GPS_NOTIFICATION_ID, n, idOut);
      } else {
        mNotificationService.cancelNotification(mContext.getPackageName(), GPS_NOTIFICATION_ID);
      }
    } catch (android.os.RemoteException ex) {
      // well, it was worth a shot
    }
  }