Notification created by IntentService uses always a wrong Intent

白昼怎懂夜的黑 提交于 2019-12-03 10:43:02

问题


Problem

When the user presses Send "Button 1"(scroll down to see the construction of the app) a new Notification is created from the RefreshService. If the user presses this notification a MainActivity instance gets started and receives a String with the value Button 1 over the Intent.

This value gets displayed.

When the user presses now the Send "Button 2" a new Notification is created from the RefreshService. If the user presses this notification a MainActivity instance gets started and receives a String ALSO with the value Button 1 over the Intent.

So as you can guess, normally there should be the value Button 2.

When the first Button the user pressed was Send "Button 2" then there would allways Button 2 be sent.

The only sollution to get the value of the second button is to restart the phone and pressing the second button first. Even force close doesn't work.

I know that I also can change the UI in another way. But I need this approach in a app where I need to restart the 'MainActivity' with another Intent so the approach should be the same.

Construction

  • A Activity called MainActivity

  • A IntentService called RefreshService

MainActivity

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

App Layout


回答1:


My suspicion is that, since the only thing changing in the Intent is the extras, the PendingIntent.getActivity(...) factory method is simply re-using the old intent as an optimization.

In RefreshService, try:

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

See:

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



来源:https://stackoverflow.com/questions/18049352/notification-created-by-intentservice-uses-always-a-wrong-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!