2016-07-15 63 views
-2

这是代码的问题的一部分:意图之前只允许我用一个活动,没有允许的AsyncTask

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()); 
} 

该工程的PendingIntent很大,但我改变CalcularHorariosDisponiblesComplejo.class了的AsyncTask。现在我所说的sameclass这样的:

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

,现在我不能使用挂起的意图的问题,所以我试图取代这样的事情,但不工作:

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); 

哪有我从PendingIntent呼叫Asynctask?

+0

? –

+0

@MiguelBenitez我不知道,这是最好的解决方案吗?,如果你这么认为,请给出解决方案。谢谢 –

+0

解决方案是'PendingIntent.getService()' – EpicPandaForce

回答

0

声明它在清单文件:

<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> 

创建IntentService:

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

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

    } 
} 

而且你为什么不使用IntentService而是创建挂起的意图

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); 
+0

问题是我没有活动!我有一个Asynctask来打电话,没有任何活动。 –

+0

服务和通知完美无缺。唯一的问题是,点击通知服务必须调用一个Asynctask,没有一个活动之前,我已经改变了代码 –

+0

这不是一个活动,是一个IntentService:IntentService(android.app.IntentService)是一种简单的类型服务,可用于通过Intent请求处理主线程的异步工作。 –