28

问题

当用户按下Send "Button 1"(向下滚动以查看应用程序的构造)时,Notification会从RefreshService. 如果用户按下此通知,一个MainActivity实例将启动并接收String一个Button 1超过Intent.

该值被显示。

当用户现在按下Send "Button 2"a new时Notification,会从RefreshService. 如果用户按下此通知,一个MainActivity实例将启动并接收一个ALSO,其值超过.String Button 1Intent

所以你可以猜到,通常应该有 value Button 2

当用户按下的第一个 Button 时,Send "Button 2"总是Button 2会发送。

获得第二个按钮值的唯一解决方案是重新启动手机并首先按下第二个按钮。即使强制关闭也不起作用。

我知道我也可以用另一种方式更改 UI。但是我需要在需要用另一个重新启动“MainActivity”的应用程序中使用这种方法,Intent所以方法应该是相同的。

建造

主要活动

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

刷新服务

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

应用布局
应用布局

4

1 回答 1

81

我的怀疑是,由于 Intent 中唯一改变的是额外内容,因此PendingIntent.getActivity(...)工厂方法只是重新使用旧意图作为优化。

在 RefreshService 中,尝试:

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

看:

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

于 2013-08-05T02:07:31.560 回答