从 Android 通知启动时 Activity 的奇怪行为
当 C2DM 消息到达移动设备时,在处理从 BroadcastReceiver 创建的通知时,我的 Android 应用程序遇到了奇怪的行为。我正在执行的体验此行为的流程如下:
- 启动我的应用程序,显示 DashboardActivity;
- 按主页按钮或后退按钮关闭应用程序;
- 接收推送消息并创建通知;
- 点击通知项,启动BookingOfferActivity活动;
- 通过按主页或后退按钮或调用 finish() 关闭 BookingOfferActivity
- 长按主页按钮并从最近的应用程序中选择我的应用程序
- 显示 BookingOffcerActivity 而不是 DashboardActivity
BookingOfferActivity 正在使用以下代码从通知启动:
Intent notificationIntent = new Intent(context, BookingOfferActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle b = new Bundle();
b.putSerializable("booking", booking);
notificationIntent.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AppUtil.getNextPushIndexBooking(), notification);
BookingOfferActivity 在中定义AndroidManifest 是这样的:
<activity android:name=".activity.BookingOfferActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
我的 DashboardActivity 是这样定义的:
<activity android:name=".activity.DashboardActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我称其为奇怪行为的原因是我想在我的应用程序启动时启动 DashboardActivity 或由用户重新启动,而不是由通知项重新启动。因此,在关闭、完成或销毁 BookingOfferActivity 并通过访问最近打开的应用程序(长按主页按钮)或访问应用程序列表上的启动器图标由应用程序重新打开后,我希望看到仪表板。
我创建了一个与此类似的问题,但我没有提供太多详细信息,因此我关闭了另一个问题并使用更多数据打开了这个问题。
非常感谢您提供有关如何解决此问题的指导
T
I am experiencing a strange behavior with my android application when dealing with a notification that is created from a BroadcastReceiver when a C2DM message arrives on the mobile device. The flow I am executing to experience this behavior is the following one:
- Start my application, DashboardActivity is displayed;
- Close application either by pressing home button or back button;
- Receive the push message and create the notification;
- Click the notification item and the activity BookingOfferActivity is launched;
- Close the BookingOfferActivity either by pressing home or back button or calling finish()
- Long press home button and select my application from recent applications
- BookingOffcerActivity is displayed instead of DashboardActivity
The BookingOfferActivity is being launched from the notification using the following code:
Intent notificationIntent = new Intent(context, BookingOfferActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle b = new Bundle();
b.putSerializable("booking", booking);
notificationIntent.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AppUtil.getNextPushIndexBooking(), notification);
BookingOfferActivity is defined in the AndroidManifest this way:
<activity android:name=".activity.BookingOfferActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
and my DashboardActivity is defined like this:
<activity android:name=".activity.DashboardActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The reason I am calling it a strange behavior is that I would like to start DashboardActivity whenever my application is started or restarted by the user, not by the notification item. So after closing, finishing or destroying BookingOfferActivity and reopening by application by accessing recent opened application (long press on home button) or accessing the launcher icon on the application list, I would like to see the Dashboard.
I create a question similar to this one but I didnt provide too much details, so I closed the other one and opened this one with more data.
Many thanks for any guidance on how to solve this problem
T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,但我想在您的场景中,第二个活动只是转到活动堆栈的顶部,并将显示直到应用程序不会被终止。我们有大致相同的问题并以简单的方式解决它:通知启动主要(第一个)活动并额外“立即转到第二个”。在第一个活动的
onCreate
中检查此额外内容,并根据需要启动第二个活动。使用此解决方案时,您应该小心按后退按钮 - 如果您不想在“通知开始”的情况下显示第一个活动,您应该在第一个活动的onResume
中关注它I'm not sure, but I suppose that in your scenario second activity just go to the top of activity stack and will be shown until application wont be killed. We have approximately same problem and solv it in easy way: Notification start main(first) activity with extra "go to second immediately". in
onCreate
of first activity check this extra and start second activity if needed. With this solution you should be careful with pressing back button - if you dont want to show first activity in case of "notification start" you should care about it inonResume
of first activity