IntentService创建的通知始终使用错误的Intent [英] Notification created by IntentService uses always a wrong Intent

查看:108
本文介绍了IntentService创建的通知始终使用错误的Intent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下发送按钮1" (向下滚动以查看应用程序的结构)时,会出现一个新的 Intent 上的值为Button 1.

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.

此值被显示.

当用户现在按下发送按钮2" 时,将出现一个新的 Notification 是从RefreshService创建的.如果用户按此通知,则MainActivity实例将启动并收到 ALSO ,且 Intent上的值为Button 1 .

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.

因此,您可能会猜到,通常应该有值Button 2.

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

当用户按下的第一个按钮是发送按钮2" 时,就会一直发送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.

我知道我也可以用其他方式更改UI.但是我在需要使用另一个 Intent Intent 因此方法应该相同.

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.

  • 名为MainActivity

称为RefreshService

MainActivity

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

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

推荐答案

我的怀疑是,由于Intent唯一改变的是附加功能,因此PendingIntent.getActivity(...)工厂方法只是将旧的intent用作优化.

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.

在RefreshService中,尝试:

In RefreshService, try:

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

请参阅:

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

这篇关于IntentService创建的通知始终使用错误的Intent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆