为什么我的android通知一直消失?

时间:2012-08-25 11:38:36

标签: android service notifications

我在android中的通知有问题我不知道我做错了什么... 此通知在我的Service类(AfterBootService)中创建,并且在以下两个类的Receiver类(MyReceiver)代码中完成引导时启动该服务:

MyReceiver.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, AfterBootService.class);
        context.startService(service);      
    }

}

AfterBootService.class

public class AfterBootService extends Service { 
    private NotificationManager mNotificationManager;
    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onCreate() {        
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        showNotification();     
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {      
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        mNotificationManager.cancel(NOTIFICATION_ID);
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
    }

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

    /**
     * Show a notification while this service is running.
     */
    private void showNotification() {

        int icon = R.drawable.icon_notification_ribbon;
        String tickerText = "Your Notification";
        long when = System.currentTimeMillis();     

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

        String expandedText = "Sample Text";
        String expandedTitle = "Program Name Here"; 

        Context context = getApplicationContext();

        Intent notificationIntent = new Intent(this, IconActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);        

        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        //notification.flags |= Notification.FLAG_NO_CLEAR;

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(context, expandedTitle, expandedText, contentIntent);               

        // Send the notification.
        mNotificationManager.notify(NOTIFICATION_ID, notification);

    }
}

现在,当我启动一切正常时,接收器启动服务和服务启动通知但是当它显示在状态栏中并且在通知窗口中,并且在一些(2-3)秒后显示正确消息IT DISAPPEAR ...我没有'设置任何允许该消息消失的东西(我想我不是专家)。我想知道的是:

  1. 如何在我的服务运行时始终保留该通知消息?
  2. 可以在不运行启动该通知的服务的情况下保留所有时间通知吗?为了更仔细地解释它,可以在使用stopSelf()破坏服务之后启动服务中的通知并仍然保留该通知消息?
  3. 如果需要,我可以在此处粘贴我的清单。 THX

1 个答案:

答案 0 :(得分:2)

编辑你的破坏方法

从:

    public void onDestroy() {
    mNotificationManager.cancel(NOTIFICATION_ID);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
}

public void onDestroy() {
    super.onDestroy();

}

因为它取消了你的通知,它会一直保留到用户清除它为止;)

如果没有来自reciver的服务,你可以触发notiffication,并且可以添加

   notification.flags = Notification.FLAG_ONGOING_EVENT;

将其设置为不可清除