1

我的应用程序将创建通知以通知用户

当应用程序处于后台进程 8 小时时

我想要的只是切换创建通知的进程(背景)

到前台,我该怎么办?

    // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    // intent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification;

    if (Build.VERSION.SDK_INT >= 16) {

        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentIntent(pendingIntent);
        builder.setContentTitle(xx);
        builder.setContentText(xx);
        builder.setTicker(xx);
        builder.setLargeIcon(xx);
        builder.setSmallIcon(xx);
        builder.setDefaults(Notification.DEFAULT_ALL);
        notification = builder.build();

    } else {

        notification = new Notification(xx, xx, 0);
        notification.defaults |= Notification.DEFAULT_ALL;
        notification.setLatestEventInfo(this, xx, xx, pendingIntent);

    }

在此处输入图像描述

在此处输入图像描述

谢谢

4

1 回答 1

3

解决方案

    Intent intent = new Intent(this, StartWorkingActivity2.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

这会将现有任务带到前台,而无需实际创建新 Activity。如果您的应用程序没有运行,它将创建一个 MyRootActivity 实例并启动它。

用户单击主页按钮后将应用程序置于最前面

于 2013-02-12T07:03:42.333 回答