onGoing通知在几秒钟后消失

时间:2012-06-21 20:29:24

标签: android service notifications

我正在尝试在服务启动时在状态栏中发出通知并将其保留在那里直到我停止服务但在几秒钟后(大约10)消失。关于我缺少什么的任何建议?这在我尝试使用notification.builder重写以与api 15兼容之前有效。日志条目显示onDestroy在我停止服务之前不会被调用,因此它仍在运行。

public class MyService extends Service {
    private NotificationManager mNM;
    private int NOTIFICATION = R.string.service_started;

public void onCreate() {
    super.onCreate();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("MyService", "Service Started");
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    mNM.cancel(NOTIFICATION);
    Log.e("MyService", "Service Ended");
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IBinder mBinder = new LocalBinder();

private void showNotification() {

    Notification.Builder builder = new Notification.Builder(getApplicationContext());
    builder.setAutoCancel(false)
           .setOngoing(true)
           .setSmallIcon(R.drawable.myicon)
           .setTicker(getText(R.string.service_label))
           .setWhen(System.currentTimeMillis())
           .setContentTitle(getText(R.string.service_started))
           .setContentText(getText(R.string.service_label));
    Notification notification = builder.getNotification();
    mNM.notify(NOTIFICATION, notification);
}

1 个答案:

答案 0 :(得分:3)

我在新手机上的ICS中正在消失的通知时遇到了同样的问题。应用程序和通知在我之前测试过的每个Android版本中都运行得很好,甚至可以在ICS模拟器上运行。毋庸置疑,这让我疯狂了几个月,但我终于找到了答案。

http://code.google.com/p/android/issues/detail?id=21635

我正在使用BroadcastReceiver监视手机上的来电,除了设置通知外,我还会在切换按钮时以编程方式启用接收器。所以我写了一个小测试应用程序,连接相同的BroadcastReceiver,并能够重现问题。我注释掉了setComponentEnabledSetting调用,通知不再消失。

相关问题