我知道,这种类型的问题有好几个,但我都试过了,还是不行。
好的,对于我的应用程序;我有一个活动。在这个 Activity 中,有 4 个 Tabs,第 4 个 Tabs 包含一个列表和一个记录按钮。当我推送记录时,会启动一个 GPS 监听器。在获得新的 gps 值后,它会将其推送到列表中。
到目前为止,这有效!
如果我单击主页按钮,它仍然可以工作,如果我长按它。它会在特定选项卡打开的情况下恢复该活动,并且列表仍然包含列表项,并且 gps 侦听器仍然处于活动状态。
这也很好用!
现在我想添加一个通知,它将列表中 gps 值的计数显示为 .number。在每个新的 gps 信号上,它都会使用新号码更新通知图标。这没问题,但是单击通知的操作完全搞砸了我的应用程序。
实际代码如下所示:
public void callNotify(String text) {
notif = new Notification();
Intent contentIntent = new Intent(this, tabactivity.class);
contentIntent.putExtra("fid", this.specialvalue);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notif.icon = R.drawable.icon;
notif.setLatestEventInfo(this, getString(R.string.app_name), text,
PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
notif.ledARGB = Color.RED;
notif.ledOnMS = 200;
notif.ledOffMS = 200;
notif.flags = Notification.FLAG_SHOW_LIGHTS
| Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_ONLY_ALERT_ONCE;
notif.number = notifyNumber;
mNotificationManager.notify(notifyNumber, notif);
}
public void updateNotify(String text) {
notifyNumber++;
notif.number = (int) (notifyNumber / 2);
Intent contentIntent = new Intent(this, tabactivity.class);
contentIntent.putExtra("fid", this.specialvalue);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notif.setLatestEventInfo(this, getString(R.string.app_name), text,
PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
mNotificationManager.cancel((notifyNumber - 2));
mNotificationManager.notify(notifyNumber, notif);
}
因此,updateNotify() 在新的 gps 信号上被调用。callNotify() 是第一个,在它启动 gps 侦听器之前。而且,是的, notifyNumber/2 是我的意图,因为我会进一步使用该号码。
如果我像这样编译它,然后单击通知,它会在第一个选项卡上打开一个新的选项卡活动。如果我点击回去,我会收到很多错误(数据库仍然打开,空指针和其他东西)。我认为因为它启动了一个新的 tabactivity 而另一个仍然打开,因为我可以看到 gps 侦听器仍在工作。
所以,我想要的是,我可以执行以下操作:我打开我的应用程序,转到 tabactivity,打开选项卡 4,单击记录。如果我当时点击,它应该隐藏应用程序,如果我只是点击主页按钮。但是有通知。如果我点击那个,它应该只是再次显示隐藏的活动,就是这样。那么,我在那里做错了什么?我想,Flags FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_SINGLE_TOP 应该可以解决问题吗?