0

我创建了两个应用程序。

一个应用程序是消息接收者 ( app1 ),另一个应用程序 ( app2 ) 用于根据消息执行其他任务。

第一个应用程序 (app1) 收到一条消息,创建通知并显示在顶部。当用户单击通知时,它会调用另一个应用程序(app2)来根据消息执行其他任务。

如果应用程序 (app2) 未运行,则应启动它。如果它已经在运行,则应显示该实例并完成要完成的任务。

我正在使用以下代码:

protected void displayNotification() {

        Notification notification = new Notification(icon, tickerText, when);
        Bundle xtra = new Bundle();

        Intent ntent = new Intent(Intent.ACTION_SEND);
        ntent.setClassName("com.example.mytestapp",
                "com.example.mytestapp.MainActivity");

        xtra.putString("id", "8610B0DD");
        xtra.putParcelable("message", msg);

        ntent.putExtras(xtra);
        ntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                ntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, contentTitle, contentText,
                pendingIntent);
        final int button_Click = 1;
        nm.notify(button_Click, notification);
}

这工作正常,但它创建另一个应用程序(app2)的多个实例

有没有办法防止创建这个多个副本?

4

2 回答 2

0

与此代码完美配合。

            Intent ntent = new Intent();
            ntent.setClassName("com.project.test",
                    "com.project.test.MainActivity");
            ntent.setType("vnd.android-dir/mms-sms");

            ntent.putExtras(bundle);

            int flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP;
            ntent.setFlags(flags);

            //startActivity(ntent);
于 2011-06-24T14:38:50.883 回答
0

您是否尝试为活动的启动模式设置“singleTask”或“singleInstance”? http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

于 2011-06-14T02:43:49.720 回答