Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在通知前检测是否显示?_Java_Android_Notifications - Fatal编程技术网

Java 如何在通知前检测是否显示?

Java 如何在通知前检测是否显示?,java,android,notifications,Java,Android,Notifications,我想知道如何使用NotificationCompat.Builder检测通知是否已启用调用.cancelAll 停止/取消存储: 通知 我使用SharedReferences,并且仅在以下情况下才调用我的通知: 日志: 08-15 14:38:50.315 25990-25990/? E/dalvikvm﹕ >>>>> Normal User 08-15 14:38:50.315 25990-25990/? E/dalvikvm﹕ >>>>

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

停止/取消存储:

通知

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

日志:

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)

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

如果您的服务从未调用showNotification,notificationManager将具有空值,这就是导致服务停止时出现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());

}

有人能帮我吗?对不起,如果我问错了问题,我的英语不是很好,android'm new您可以呼叫cancelAll,无论通知是否显示。我试着打电话给notificationManager.cancelAll,我的应用程序停止了:你能编辑你的问题并发布你在应用程序崩溃时得到的日志吗?或者发布一些代码,显示你在哪里打电话。谢谢你回复@adelphus。我把代码,或者部分代码放进去了。非常感谢你@adelphus。我不能说:如果通知经理!=null notificationManager.cancelAll;而不是它?是吗?是的,那也行。但请确保在使用notificationManager的任何地方都这样做。
if(Hello.CONFIG_APP.getBoolean("show_notification", true)) showNotificacion();
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)
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
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());

}