0

Hi guys I'm at the beginning with Android and I'm trying to make a notify from a class different from my main activity. But e.printStackTrace() say "null" and it stop at line : "NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);" If I make the same notification from the mainActivity all goes well. Can you help me please?

if(giorni_di_differenza <= 15)
{

    try{
        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
        NotificationCompat.Builder n = new NotificationCompat.Builder(context)
            .setContentTitle(nome_evento)
            .setContentText(descrizione_evento)
            .setContentIntent(pi)
            .setAutoCancel(true)
            .setLights(Color.GREEN, 1000, 1000)
            .setSmallIcon(R.drawable.ic_launcher);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, n.build());

    }catch(Exception e){
        e.printStackTrace();
    }
}

If you need more code I can send you.

LogCat: http://pastebin.com/W4hKbf6W (the Pause GC error is an error due to Samsung stock ROM)

1
  • ok, one second @kalyanpvs Commented Oct 31, 2014 at 11:14

2 Answers 2

3

you need Context for accessing NotificationManager from Outside Activity

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

and also your logcat clearly said

java.lang.IllegalStateException: System services not available to Activities before onCreate()

You can access NotificationManager only after onCreate() of Activity

Sign up to request clarification or add additional context in comments.

5 Comments

it works, but, when the debug stop, the notification disappear :0
it works ok, the problem was that I did two notifications, and so...how can I do all two ? @MD
@PierpaoloErcoli I did not get it. What are you talk in about?
if I call two different notifications, for example one to notify that GPS is turned off, and another that notify anything else. My app notify the second.
@PierpaoloErcoli For that you'll have to change NotificationID at notificationManager.notify(0, n.build()); as 0 to 1
2

ya because it doesn't find context in your class:

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

do it like this:

or

//pass different id for different notifications
private void showNotification(Context con, int notificationID) {
    if(giorni_di_differenza <= 15)
    {

        try{
            PendingIntent pi = PendingIntent.getActivity(con, 0, new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
            NotificationCompat.Builder n = new NotificationCompat.Builder(con)
                .setContentTitle(nome_evento)
                .setContentText(descrizione_evento)
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setLights(Color.GREEN, 1000, 1000)
                .setSmallIcon(R.drawable.ic_launcher);
        NotificationManager notificationManager = (NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(notificationID, n.build());

    }catch(Exception e){
        e.printStackTrace();
    }
}
}

3 Comments

it works ok, the problem was that I did two notifications, and so...how can I do all two ? @Hamad
change this: notificationManager.notify(0, n.build()); to this notificationManager.notify(1, n.build());
wait I have another problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.