【问题标题】:Why my android notification all time disappear?为什么我的android通知总是消失?
【发布时间】:2012-08-25 19:59:03
【问题描述】:

我在 android 中的通知有问题我不知道我做错了什么... 此通知是在我的服务类 (AfterBootService) 中创建的,并且该服务在启动完成时在两个类下面的接收器类 (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) 秒后消失...我没有t 设置任何允许该消息消失的东西(我想我不是专家)。我想知道的:

  1. 如何在我的服务运行时始终保留该通知消息?
  2. 是否可以在不运行启动该通知的服务的情况下保留所有时间通知?为了更仔细地解释它,可以在使用 stopSelf() 销毁服务之后在服务中启动通知并仍然保留该通知消息?

如果需要,我可以在此处粘贴我的清单。谢谢

【问题讨论】:

    标签: android service notifications


    【解决方案1】:

    编辑你的销毁方法

    来自:

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

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

    因为它会取消您的通知,并且它会一直保留到用户清除它;)

    您可以在没有接收方服务的情况下触发通知,并且可以添加

       notification.flags = Notification.FLAG_ONGOING_EVENT;
    

    将其设置为不可清除

    【讨论】:

    • 是的,现在它工作了,但我也犯了另一件事,现在我试着解释一下:我的程序工作了,但只有一次,这里我做了什么: 1.我按下从虚拟中擦除所有信息。 2.当擦除我关闭它并按下运行->我的程序,在这里我得到了消息,毕竟完成... .apk 安装成功。 3. 现在我按下 Window->Android AVD Manager 并在 .apk 安装后启动了 MyVirtual 并且我的程序正常工作(没有错过通知)。 4.我关闭了那个 Virtual,然后我再次按下 Run->MyProgram,现在发生了什么我有错误(不是成功安装)。
    • 但是现在当我将代码更改为 U 编写的代码时,即使我第二次按下 Run->MyApplication 我也没有收到错误,并且我的 .apk 再次安装成功:)。现在,当我需要查看我的程序如何工作时,我需要从 Window->AVD Manager 而不是从 Run->MyAplication 启动 MyVirtual,因为我没有进行任何更改并且我已经安装了我的 .APK。这是我的错(当我搜索我做错了什么时),因为我一直按 Run->MyAplication 而不是 Window->AVD Manager 并且我的通知因为那个和错误的代码而消失了。
    • 现在一切都像我想要的那样工作:) 我需要明白,当我对代码进行更改时,我需要按下并安装另一个 .apk。当我没有更改任何内容时,我只需转到 Window->AVD Manager 并从这里简单地启动 MyVirtual。无论如何我的代码是错误的,Ur 很好:) thx
    • 从来没有提到我很高兴我能够帮助你.. :)
    猜你喜欢
    • 2022-10-15
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多