-1

有没有办法在 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>
4

2 回答 2

1

伙计们,我很抱歉,但这是我的错:| 几天前,我决定更改我的应用程序包的名称,却忘记从手机中删除旧应用程序。我没有意识到它会在我的手机上导致两个单独的安装,任务管理器只显示应用程序名称,所以看起来确实有同一个应用程序的两个实例,实际上每个实例都有一个;)抱歉麻烦并感谢您愿意提供帮助:)

于 2011-03-09T22:42:30.533 回答
0

安卓文档说:

如下表所示,这些模式分为两大类,一侧是“standard”和“singleTop”活动,另一侧是“singleTask”和“singleInstance”活动。可以多次实例化具有“标准”或“单顶”启动模式的活动。实例可以属于任何任务,并且可以位于活动堆栈中的任何位置。通常,它们会被启动到调用 startActivity() 的任务中(除非 Intent 对象包含 FLAG_ACTIVITY_NEW_TASK 指令,在这种情况下会选择不同的任务 - 请参阅 taskAffinity 属性)。相反,“singleTask”和“singleInstance”活动只能开始一个任务。它们始终位于活动堆栈的根部。而且,

所以看起来(虽然我没有检查过)singleInstance 就是你要找的东西。

见:http ://bit.ly/gH8SBb

于 2011-03-09T19:48:11.427 回答