gpt4 book ai didi

android - Notify 导致对象在 notify() 之前未被线程锁定

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:35 26 4
gpt4 key购买 nike

我正在尝试在后台服务中启动通知,这也是位置监听器。我有一个功能:

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;
}

然后在 onStart 中调用:

createNotification.notify();

但是我得到以下错误:

“对象在 notify() 之前未被线程锁定”

我该如何解决这个问题?它实际上需要被调用一次并继续运行。

最佳答案

notify和wait是并发使用的两个java方法,为了使用android通知你必须调用

Notification noti = CreateNotification();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);

有关更多引用资料,请参阅 http://www.vogella.com/tutorials/AndroidNotifications/article.html

关于android - Notify 导致对象在 notify() 之前未被线程锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25295310/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com