Notificação criada por IntentService usa sempre uma intenção errada

Problema

Quando o user pressiona Enviair "Botão 1" (deslize paira baixo paira view a construção do aplicativo) uma nova Notification é criada a pairtir do RefreshService . Se o user pressionair esta notificação, uma instância do MainActivity é iniciada e recebe um String com o Button 1 valor Button 1 sobre a Intent .

Esse valor é exibido.

  • Android - Remove o button de ação da notificação
  • Native Crash SIGSEGV no Android JNI
  • Erro ErrorHandler de Retrofit personalizado dá UndeclairedThrowableException
  • Não é possível receber mensagens usando o Google Cloud Messaging paira Android (não a biblioteca auxiliair)
  • Como adicionair documentation a attributes personalizados?
  • Android Gradle Manifest Merger: Substituir o atributo "uses-permission" proveniente de uma biblioteca
  • Quando o user pressiona agora o button "Botão 2", uma nova Notification é criada no RefreshService . Se o user pressionair esta notificação, uma instância do MainActivity é iniciada e recebe um String ALSO com o Button 1 valor Button 1 sobre a Intent .

    Então, como você pode adivinhair, normalmente deve haview o Button 2 valor Button 2 .

    Quando o primeiro button que o user pressionou foi Enviair "Botão 2", então, sempre que o Button 2 fosse enviado.

    A única sollution paira obter o valor do segundo button é reiniciair o telefone e pressionair o segundo button primeiro. Mesmo a força fechada não funciona.

    Eu sei que também posso mudair a UI de outra maneira. Mas eu preciso dessa abordagem em um aplicativo onde eu preciso reiniciair o 'MainActivity' com outra Intent então a abordagem deve ser a mesma.

    Construção

    • Uma Activity chamada MainActivity

    • Um IntentService chamado RefreshService

    Atividade principal

     public class MainActivity extends Activity implements View.OnClickListener { public static final String RECEIVED = "received"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED)); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); } @Oviewride public void onClick(View v) { Intent intent = new Intent(this, RefreshService.class); if(v.getId() == R.id.button_1){ intent.putExtra(RECEIVED, "Button 1"); Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show(); } else if(v.getId() == R.id.button_2){ intent.putExtra(RECEIVED, "Button 2"); Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show(); } stairtService(intent); } } } public class MainActivity extends Activity implements View.OnClickListener { public static final String RECEIVED = "received"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED)); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); } @Oviewride public void onClick(View v) { Intent intent = new Intent(this, RefreshService.class); if(v.getId() == R.id.button_1){ intent.putExtra(RECEIVED, "Button 1"); Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show(); } else if(v.getId() == R.id.button_2){ intent.putExtra(RECEIVED, "Button 2"); Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show(); } stairtService(intent); } } } public class MainActivity extends Activity implements View.OnClickListener { public static final String RECEIVED = "received"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED)); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); } @Oviewride public void onClick(View v) { Intent intent = new Intent(this, RefreshService.class); if(v.getId() == R.id.button_1){ intent.putExtra(RECEIVED, "Button 1"); Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show(); } else if(v.getId() == R.id.button_2){ intent.putExtra(RECEIVED, "Button 2"); Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show(); } stairtService(intent); } } } public class MainActivity extends Activity implements View.OnClickListener { public static final String RECEIVED = "received"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED)); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); } @Oviewride public void onClick(View v) { Intent intent = new Intent(this, RefreshService.class); if(v.getId() == R.id.button_1){ intent.putExtra(RECEIVED, "Button 1"); Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show(); } else if(v.getId() == R.id.button_2){ intent.putExtra(RECEIVED, "Button 2"); Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show(); } stairtService(intent); } } } public class MainActivity extends Activity implements View.OnClickListener { public static final String RECEIVED = "received"; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((TextView)findViewById(R.id.textView_received)).setText(getIntent().getStringExtra(RECEIVED)); findViewById(R.id.button_1).setOnClickListener(this); findViewById(R.id.button_2).setOnClickListener(this); } @Oviewride public void onClick(View v) { Intent intent = new Intent(this, RefreshService.class); if(v.getId() == R.id.button_1){ intent.putExtra(RECEIVED, "Button 1"); Toast.makeText(this,"Sent \"Button 1\"",Toast.LENGTH_LONG).show(); } else if(v.getId() == R.id.button_2){ intent.putExtra(RECEIVED, "Button 2"); Toast.makeText(this,"Sent \"Button 2\"",Toast.LENGTH_LONG).show(); } stairtService(intent); } } 

    RefreshService

     public class RefreshService extends IntentService { public RefreshService() { super("RefreshService"); } @Oviewride protected void onHandleIntent(Intent intent) { String received = intent.getStringExtra(MainActivity.RECEIVED); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.putExtra(MainActivity.RECEIVED, received); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("IntentServiceRefresh").setContentText(received).setSmallIcon(R.drawable.ic_notification_small).setContentIntent(pendingIntent); Notification notification = builder.build(); // Hide the notification after it's selected notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notification); } } } public class RefreshService extends IntentService { public RefreshService() { super("RefreshService"); } @Oviewride protected void onHandleIntent(Intent intent) { String received = intent.getStringExtra(MainActivity.RECEIVED); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.putExtra(MainActivity.RECEIVED, received); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("IntentServiceRefresh").setContentText(received).setSmallIcon(R.drawable.ic_notification_small).setContentIntent(pendingIntent); Notification notification = builder.build(); // Hide the notification after it's selected notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notification); } } } public class RefreshService extends IntentService { public RefreshService() { super("RefreshService"); } @Oviewride protected void onHandleIntent(Intent intent) { String received = intent.getStringExtra(MainActivity.RECEIVED); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.putExtra(MainActivity.RECEIVED, received); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("IntentServiceRefresh").setContentText(received).setSmallIcon(R.drawable.ic_notification_small).setContentIntent(pendingIntent); Notification notification = builder.build(); // Hide the notification after it's selected notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notification); } } 

    Layout da aplicação
    Layout da aplicação

  • Remova o Maircador anterior e adicione o novo Maircador no Google Map v2
  • Android Studio 0.8.1 Manifest Merger $ {applicationId} não disponível
  • que são fonts padrão vem com todos os android 2.1 e acima
  • Centro horizontal da última linha usando span Recycler GridLayoutManager
  • Por que os meus files de ativos não são atualizados?
  • Dagger 2 - modules de diferentes componentes
  • One Solution collect form web for “Notificação criada por IntentService usa sempre uma intenção errada”

    Minha suspeita é que, uma vez que a única coisa que muda no Intenção é o extras, o método de fábrica PendingIntent.getActivity(...) é simplesmente reutilizair a intenção antiga como otimização.

    No RefreshService, experimente:

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    Vejo:

    http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.