我将尝试在后台服务中启动通知,该服务还是位置监听器。 我有一个功能:
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()之前,对象没有被线程锁定”。我该如何解决这个问题?它只需要被调用一次,并持续运行即可。