IntentService 创建的通知总是使用错误的 Intent

2023-12-20

Problem

When the user presses Send "Button 1"(scroll down to see the construction of the app) a new Notification http://developer.android.com/reference/android/app/Notification.html is created from the RefreshService. If the user presses this notification a MainActivity instance gets started and receives a String http://docs.oracle.com/javase/6/docs/api/java/lang/String.html with the value Button 1 over the Intent http://developer.android.com/reference/android/content/Intent.html.

显示该值。

When the user presses now the Send "Button 2" a new Notification http://developer.android.com/reference/android/app/Notification.html is created from the RefreshService. If the user presses this notification a MainActivity instance gets started and receives a String http://docs.oracle.com/javase/6/docs/api/java/lang/String.html ALSO with the value Button 1 over the Intent http://developer.android.com/reference/android/content/Intent.html.

正如你可以猜到的,通常应该有这个值Button 2.

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

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

我知道我还可以用另一种方式更改用户界面。但我需要在应用程序中使用这种方法,我需要用另一个应用程序重新启动“MainActivity”Intent http://developer.android.com/reference/android/content/Intent.html所以方法应该是相同的。

建造

  • A Activity http://developer.android.com/reference/android/app/Activity.html called MainActivity

  • A IntentService http://developer.android.com/reference/android/app/IntentService.html called RefreshService

主要活动

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

App Layout
App Layout


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

在 RefreshService 中,尝试:

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

See:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IntentService 创建的通知总是使用错误的 Intent 的相关文章

随机推荐