【问题标题】:How to detect if shown before a Notification?如何检测是否在通知之前显示?
【发布时间】:2015-08-15 17:39:36
【问题描述】:

我想知道如何使用 NotificationCompat.Builder 检测通知是否启用调用 .cancelAll()。

停止/onDestroy:

@Override
    public void onDestroy(){
        stopMediaPlayer(); //Line 114

    }

public void stopMediaPlayer() {
        notificationManager.cancelAll();//Line 225
        mMediaPlayer.release();
    }

通知

public void showNotification(){

        builder = new NotificationCompat.Builder(this);

        builder.setTicker("Hello");
        builder.setContentTitle("Helloo");
        builder.setContentText("Helloooo");
        builder.setOngoing(true);

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIFICATION_ID, builder.build());

    }

我使用 SharedPreferences 并且仅在以下情况下调用我的通知:

if(Hello.CONFIG_APP.getBoolean("show_notification", true)) showNotificacion();

Logcat:

08-15 14:38:50.315  25990-25990/? E/dalvikvm﹕ >>>>> Normal User
08-15 14:38:50.315  25990-25990/? E/dalvikvm﹕ >>>>> com.myapptest [ userId:0 | appId:10223 ]
08-15 14:39:06.492  25990-25990/? E/MediaPlayer﹕ Should have subtitle controller already set
08-15 14:39:33.051  25990-25990/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapptest, PID: 25990
    java.lang.RuntimeException: Unable to stop service com.myapptest.MyMPServ@423a8fd0: java.lang.NullPointerException
            at android.app.ActivityThread.handleStopService(ActivityThread.java:3036)
            at android.app.ActivityThread.access$2300(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.myapptest.MyMPServ.stopMediaPlayer(MyMPServ.java:225)
            at com.myapptest.MyMPServ.onDestroy(MyMPServ.java:114)
            at android.app.ActivityThread.handleStopService(ActivityThread.java:3019)
            at android.app.ActivityThread.access$2300(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 谁能帮帮我?对不起,如果我问错了问题(我不太擅长英语和 android 是新的)
  • 无论通知是否显示,您都可以拨打cancelAll()This question 可能会帮助你。
  • 我尝试调用 notificationManager.cancelAll() 并且我的应用停止了:(
  • 您可以编辑您的问题并发布您在应用程序崩溃时获得的 logcat 吗?或发布一些代码,显示您在哪里调用cancelAll()
  • 感谢您回答@adelphus。我放了代码,或者其中的一部分。

标签: java android notifications


【解决方案1】:

好的 - 看起来当您的服务停止时,notificationManager 尚未初始化。看起来您只是在调用 showNotification() 时才初始化 notificationManager:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

如果您的服务从不调用showNotification(),则notificationManager 将具有null 值,这就是您的服务停止时导致NullPointerException (NPE) 的原因。

将系统服务的值保存为类中的字段通常不是一个好主意。相反,删除 NotificationManager 字段并在您需要使用它的每个方法中创建一个新的NotificationManager 变量。像这样:

public void stopMediaPlayer() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancelAll();
    mMediaPlayer.release();
}

public void showNotification(){

    builder = new NotificationCompat.Builder(this);

    builder.setTicker("Hello");
    builder.setContentTitle("Helloo");
    builder.setContentText("Helloooo");
    builder.setOngoing(true);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Will display the notification in the notification bar
    notificationManager.notify(NOTIFICATION_ID, builder.build());

}

【讨论】:

  • 非常感谢@adelphus。我不能把:if (notificationManager != null) notificationManager.cancelAll(); 代替吗?对吗?
  • 是的,这也可以。但请确保在使用 notificationManager 的任何地方都这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多