一旦服务结束,通知就会消失

use*_*605 8 java notifications android

我有一个服务,可以在网站上搜索数据,然后在必要时向用户发送通知.

我遇到的问题是,一旦服务关闭(可能是即时的),通知就会消失.以下代码是应用程序的所有通知代码.我没有输入任何代码来取消通知.

public static void CreateNotificationCompat(int notificationID, String link, String title, String text, Context ctx)
{
    Intent notificationIntent = new Intent("android.intent.action.VIEW", Uri.parse(link));
    PendingIntent contentIntent = PendingIntent.getActivity(ctx.getApplicationContext(), 0, notificationIntent, 0);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    Resources res = ctx.getResources();

    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.notify_icon)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(text)
                .setAutoCancel(false);

    Notification n = builder.getNotification();

    nm.notify(notificationID, n);
}
Run Code Online (Sandbox Code Playgroud)

如果它有任何区别(很确定它没有)我在这里使用应用程序上下文.

我的服务定期启动,处理网站,如果需要通知然后关闭.

在我的服务结束时,而不是只是调用stopSelf(),我睡了30秒左右,然后调用stopSelf().然后通知停留30秒然后消失.在通知消失时,logcat中没有任何相关内容.

到目前为止,我只能使用ICS进行测试.

Mar*_*ski 2

您似乎忘记先检查开发人员手册。请参阅此处http://developer.android.com/guide/topics/ui/notifiers/notifications.html并查找 FLAG_ONGOING_EVENT 和 FLAG_NO_CLEAR 标志。

到目前为止我只能使用 ICS 进行测试。

这就是模拟器非常有用的原因。尝试一下。它是 SDK 的一部分。