我正在使用一项服务,该服务会在后端发生特定情况时向用户设备发出通知。但是现在这一次在非活动类中登录(一段成功的代码运行)。在这种情况下,我如何从非活动类发送通知???
代码:
public XMPPConnection checkXMPPConnection(String userName,String password) {
connection = new XMPPConnection(connConfig);
try {
connection.connect();
System.out.println("Logginnnngggg innn");
connection.login(userName, password);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
final PacketCollector collector = connection.createPacketCollector(filter);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
//notification(packet.getFrom());
packet = collector.nextResult();
Message message = (Message)packet;
notification(""+message.getBody(), 0);
System.out.println("Messageeeee isisssssss......."+message.getBody());
}
}, filter);
通知代码:
public void notification(CharSequence message, int notificationID) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)UserMenuActivity.context.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "ABC";
CharSequence contentText = message;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
mNotificationManager.notify(notificationID, notification);
}
以上两种方法都属于非活动类。
此方法在非活动类中。我只想在触发上述代码时发送通知现在告诉我同样的情况我该怎么办?
谢谢