Notificação criada pelo IntentService usa sempre uma intenção errada
Quando o usuário pressionaEnvie "Botão 1"(role para baixo para ver a construção do aplicativo) um novoNotification
é criado a partir doRefreshService
. Se o usuário pressionar essa notificação,MainActivity
instância é iniciado e recebe umString
com o valorButton 1
sobre oIntent
.
Este valor é exibido.
Quando o usuário pressiona agora oEnvie "Botão 2" um novoNotification
é criado a partir doRefreshService
. Se o usuário pressionar essa notificação,MainActivity
instância é iniciado e recebe umString
ALÉM DISSO com o valorButton 1
sobre oIntent
.
Então, como você pode imaginar, normalmente deve haver o valorButton 2
.
Quando o primeiro botão pressionado pelo usuário foiEnvie "Botão 2" então haveria sempreButton 2
ser enviado.
A única solução para obter o valor do segundo botão é reiniciar o telefone e pressionar primeiro o segundo botão. Mesmo a força perto não funciona.
Eu sei que também posso mudar a interface do usuário de outra maneira. Mas eu preciso dessa abordagem em um aplicativo onde eu preciso reiniciar o 'MainActivity' com outroIntent
então a abordagem deve ser a mesma.
A Activity
chamadoMainActivity
A IntentService
chamadoRefreshService
Atividade principal
public class MainActivity extends Activity implements View.OnClickListener {
public static final String RECEIVED = "received";
@Override
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);
}
@Override
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();
}
startService(intent);
}
}
RefreshService
public class RefreshService extends IntentService {
public RefreshService() {
super("RefreshService");
}
@Override
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 do aplicativo