Notifications 通知建筑商和地面开工
这是有效的:IntentService与NotificationCompat.Builder一起提供通知以与NotificationManager一起使用。notify()。需要设置ContentIntent(PendingContent)。发送notify时,通知将显示在通知中,并且是持久的(在用户单击它之前保持活动状态,此时它将启动.setContentIntent中指定的活动)。好!! 什么不起作用:我希望这项服务能像电话服务一样长期使用,所以startForeground()似乎是建议的。然而,当我包括这一点时,相关的通知确实会像它应该的那样出现在托盘中,但它不是持久的,并且在IntentService结束时消失(与上面不同)。(关联的通知还使用.setContentIntent并启动另一个活动。) 有什么想法吗?至关重要的是,服务在检测到某个(罕见的)“事件”之前不会消亡。同样重要的是,在用户单击该通知作出响应之前,该通知保持活动状态Notifications 通知建筑商和地面开工,notifications,intentservice,Notifications,Intentservice,这是有效的:IntentService与NotificationCompat.Builder一起提供通知以与NotificationManager一起使用。notify()。需要设置ContentIntent(PendingContent)。发送notify时,通知将显示在通知中,并且是持久的(在用户单击它之前保持活动状态,此时它将启动.setContentIntent中指定的活动)。好!! 什么不起作用:我希望这项服务能像电话服务一样长期使用,所以startForeground()似乎是建议的
下面是简化的代码(后一种情况):谢谢
public class SService extends IntentService {
public SService() {
super("SService");
}
@Override
protected void onHandleIntent(Intent sIntent) {
//Notification construction:
Notification notif;
Context context = this;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent bIntent = new Intent(context, BarNotifActivity.class);
PendingIntent pbIntent = PendingIntent.getActivity(context, 0, bIntent,0);
Notification barNotif;
NotificationCompat.Builder bBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.bar_title))
.setContentText(getString(R.string.bar_text))
//.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pbIntent);
barNotif = bBuilder.build();
this.startForeground(1, barNotif);
long[] vibration = {0, 300, 1000, 300, 1000, 300, 1000, 300, 1000};
Intent mIntent = new Intent(context, NotifReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, mIntent,0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.alert_text))
.setContentText(getString(R.string.alert_text))
.setTicker("**Notification Arrived!**")
.setVibrate(vibration)
//.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pIntent);
notif = mBuilder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.flags |= Notification.FLAG_INSISTENT;
try {
Thread.sleep(7000); //Pause 7 sec.
} catch (InterruptedException e) {
e.printStackTrace();
}
mNotificationManager.notify(1, notif);
}
}算了吧!很抱歉我注意到我在startForeground和mNotificationManager.notify中给出了相同的值!一旦我换了一个,一切都正常。艾伊。L