-2

I want to know how to detect whether the notification is enabled to call .cancelAll() using NotificationCompat.Builder.

Stop/onDestroy:

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

    }

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

Notification

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());

    }

I use SharedPreferences and I call my notification only if:

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)
Arnaldo
  • 673
  • 6
  • 22
  • Can anyone help me out ? Sorry if I wrong question (I 'm not very good With english and android ' m new) – Arnaldo Aug 15 '15 at 17:57
  • You are allowed to call `cancelAll()` regardless of whether the notification is showing or not. [This question](http://stackoverflow.com/questions/3630733/how-to-check-which-notifications-are-active-in-status-bar-in-android-dev) might help you out. – adelphus Aug 15 '15 at 18:28
  • I tried to call the notificationManager.cancelAll() and my app stopped :( – Arnaldo Aug 15 '15 at 18:35
  • Can you edit your question and post the logcat you get when your app crashes? Or post some code showing where you are calling `cancelAll()` – adelphus Aug 15 '15 at 18:37
  • Thanks for answering @adelphus . I put the code, or part of it. – Arnaldo Aug 15 '15 at 18:55
  • It looks like your App crash has nothing to do with your NotificationManager code. Logcat is telling you that there is a NullPointerException in `com.myapptest.MyMPServ.stopMediaPlayer (MyMPServ.java)` on line 225. Try and see why a value might be `null` on that line, or post that method in your question. – adelphus Aug 15 '15 at 19:00
  • I forgot the code : **"Stop/onDestroy"** I know it has to do with the notification because when : `Hello.CONFIG_APP.getBoolean ( " show_notification ", true)` is "true" , I do not get the error ): – Arnaldo Aug 15 '15 at 19:05

1 Answers1

0

OK - it looks like when your service is stopping, notificationManager hasn't been initialised. It looks like you are only initialising notificationManager when you call showNotification():

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

If your service never calls showNotification(), notificationManager will have a null value and that's what is causing the NullPointerException (NPE) when your service is stopping.

It's generally not a good idea to save the value of system services as fields in your class. Instead, delete the NotificationManager field and just create a new NotificationManager variable in each method you need to use it. Like this:

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
  • 10,116
  • 5
  • 36
  • 46
  • Thank You so much @adelphus . And I could not put : `if (notificationManager != null) notificationManager.cancelAll();` instead of it? It would be correct? – Arnaldo Aug 15 '15 at 19:22
  • Yes, that would work too. But make sure you do that everywhere you use `notificationManager`. – adelphus Aug 15 '15 at 19:23