arleitiss
arleitiss

Reputation: 1304

Notify causes object not locked by thread before notify()

I am trying to start up notification in background service which is also Location Listener. I have a function:

public Notification CreateNotification(){


        Intent notificationIntentStop = new Intent(this.getApplicationContext(), StopService.class);
        PendingIntent contentIntentStop = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntentStop, 0);


        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        Shortcuts shorts = new Shortcuts(this);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("Localarm Running")
                .setLargeIcon(largeIcon);
        mBuilder.addAction(R.drawable.ico, "Stop", contentIntentStop);
        mBuilder.setContentText("Awake Me Up running.");
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        Notification bui = mBuilder.build();

        bui.flags|= Notification.FLAG_NO_CLEAR;
            Intent notificationIntent = new Intent(this.getApplicationContext(), Intro.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
            bui.contentIntent = contentIntent;

        return bui;
    }

And in onStart I call:

createNotification.notify();

However I get the following error:

"object not locked by thread before notify()"

how do I fix this? It literally needs to be called once and just keep running.

Upvotes: 3

Views: 6432

Answers (3)

mmlooloo
mmlooloo

Reputation: 18977

notify and wait are two java methods that used in concurrency, in order to use android notification you must call

Notification noti = CreateNotification();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);

for more references look at http://www.vogella.com/tutorials/AndroidNotifications/article.html

Upvotes: 4

r2DoesInc
r2DoesInc

Reputation: 3771

Wrong notify() method.

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        
    notificationManager.notify(0, CreateNotification());

Upvotes: 0

Edward van Raak
Edward van Raak

Reputation: 4920

You are calling the notify of the Java Object class:

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Upvotes: 0

Related Questions