2016-07-15 61 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請求處理主線程的異步工作。 –