-2

This is the part of code with the problem:

Intent intent = new Intent(MyGcmListenerService.this, CalcularHorariosDisponiblesComplejo.class);
    intent.putExtra("idComplejo",idComplejoSeleccionado);
    intent.putExtra("idCancha",idCanchaSeleccionada);
    intent.putExtra("idUsuarioComplejo",idUsuarioComplejo);
    intent.putExtra("idPais",idPais);
    intent.putExtra("nombreComplejo", nombreComplejo);
    intent.putExtra("dia",day);
    intent.putExtra("mes",month);
    intent.putExtra("anio",year);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    MyGcmListenerService.this,
                    m,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );//creamos la notificacion
    NotificationCompat.Builder n  = new NotificationCompat.Builder(MyGcmListenerService.this)
            .setContentTitle(titulo)
            .setSound(uri)
            .setSmallIcon(R.drawable.notification_icon)
            .setLargeIcon(bm)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("El usuario " + nombreOrigen + " " + mensaje))
            .setContentIntent(resultPendingIntent)
            .setAutoCancel(true);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(m, n.build());
}

The pendingIntent works great, but I change the CalcularHorariosDisponiblesComplejo.class for a AsyncTask. Now I call the sameclass like this:

        new CalcularHorariosComplejoAsyncTask(MyGcmListenerService.this, idComplejoSeleccionado, idCanchaSeleccionada, nombreComplejo, idUsuarioComplejo, idPais, String.valueOf(day),String.valueOf(month),String.valueOf(year)).execute();

The problem that now I can't use pending intent, so I try to replace something like this but is not working:

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                MyGcmListenerService.this,
                m,
                new CalcularHorariosComplejoAsyncTask(MyGcmListenerService.this, idComplejoSeleccionado, idCanchaSeleccionada, nombreComplejo, idUsuarioComplejo, idPais, String.valueOf(day),String.valueOf(month),String.valueOf(year)).execute(),
                PendingIntent.FLAG_UPDATE_CURRENT);

How can I call from PendingIntent to a Asynctask?

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54

1 Answers1

0

Declare it in the manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.app" >
        ...
        <service android:name="YOUR_APP_PACKAGE.CalcularHorariosComplejoService">
        </service>
    </application>
</manifest>

Create the IntentService:

public class CalcularHorariosComplejoService extends IntentService {
    public CalcularHorariosComplejoService() {
        super("CalcularHorariosComplejoService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //Calculo de horarios complejos

    }
}

And create the pending intent

Intent intent = new Intent(MyGcmListenerService.this, YOUR_APP_PACKAGE.CalcularHorariosComplejoService.class);
    intent.putExtra("idComplejo",idComplejoSeleccionado);
    intent.putExtra("idCancha",idCanchaSeleccionada);
    intent.putExtra("idUsuarioComplejo",idUsuarioComplejo);
    intent.putExtra("idPais",idPais);
    intent.putExtra("nombreComplejo", nombreComplejo);
    intent.putExtra("dia",day);
    intent.putExtra("mes",month);
    intent.putExtra("anio",year);

PendingIntent pending = PendingIntent.getService(MyGcmListenerService.this, 0, intent, 0);
Miguel Benitez
  • 2,322
  • 10
  • 22
  • The problem is that I don't have Activity! I have a Asynctask to call, no an activity. – Faustino Gagneten Jul 15 '16 at 15:02
  • The services and the notifications works perfect. The only problem is that on click the notification the service have to call a Asynctask, no an Activity before I've changed the code – Faustino Gagneten Jul 15 '16 at 15:03
  • It's not an activity, is an IntentService: IntentService (android.app.IntentService) is a simple type of service that can be used to handle asynchronous work off the main thread by way of Intent requests. – Miguel Benitez Jul 15 '16 at 15:03
  • OK, but I have an error: "CalcularHorariosComplejoService is not an enclosing class" – Faustino Gagneten Jul 15 '16 at 15:11
  • My fault: PendingIntent pending = PendingIntent.getService(MyGcmListenerService.this, 0, intent, 0); – Miguel Benitez Jul 15 '16 at 15:13
  • Now I have a problem: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" When the phone receive the notfication explote. Also don't let me touch the notification – Faustino Gagneten Jul 15 '16 at 15:18
  • Try intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Also If you can provide me the logcat It could be useful – Miguel Benitez Jul 15 '16 at 15:40
  • Anyway here It's a solution using Notifications without Activity and using intent service. http://stackoverflow.com/questions/36433573/notification-action-without-starting-new-activity Hope works. Suerte – Miguel Benitez Jul 15 '16 at 15:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117433/discussion-between-federick-jons-and-miguel-benitez). – Faustino Gagneten Jul 15 '16 at 16:22