有没有办法在 c2dm 消息后从通知栏中唤醒已经在运行的应用程序?我有这个注册到 c2dm 服务器的应用程序,它从我的服务器接收推送通知以进行一些处理。因此,在我从服务器收到 c2dm 消息后,它会向用户显示状态栏通知,用户展开通知并单击我的应用程序,将其打开。
一切都很好,但如果这个应用程序之前已经运行过(从图标开始),这会将我的应用程序的另一个实例加载到内存中。还有一些东西在里面崩溃。我已经改变了android:launchMode="singleTop"
我所有的活动,我尝试intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
在我的通知中使用,但没有运气。我总是最终运行 2 个应用程序。
任何帮助表示赞赏
这是我在收到 c2dm 消息后用来创建通知的静态函数:
public static void notifyStart(Context context, String notificationText) {
//notification
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon_notify;
CharSequence tickerText = notificationText;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 400;
notification.ledOffMS = 400;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
CharSequence contentTitle = "App Name";
CharSequence contentText = notificationText;
Intent notificationIntent = new Intent(context, home.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1350, notification);
}
这是我的家庭活动:
<activity android:name=".home"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>