0

我正在使用一项服务,该服务会在后端发生特定情况时向用户设备发出通知。但是现在这一次在非活动类中登录(一段成功的代码运行)。在这种情况下,我如何从非活动类发送通知???

代码:

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

    }

以上两种方法都属于非活动类。

此方法在非活动类中。我只想在触发上述代码时发送通知现在告诉我同样的情况我该怎么办?

谢谢

4

2 回答 2

1

可能有几种选择:

  • 向您的checkXMPP方法添加参数:参数将是一个上下文。这通常是最简单的,但会导致大量代码与上下文相关联,因此耦合度更高。实际上,在这里您需要为数据包侦听器提供上下文。

  • 您还可以启动自己的IntentService,它将通过 NotificationManager 更新通知。使用 Intent 启动服务。这看起来很困难,但实际上并非如此。

这是一个例子:http ://androidhotel.wordpress.com/2011/12/14/a-simple-notification-system-with-the-intentservice-part-i/

于 2012-11-05T07:10:08.360 回答
0

这是我是如何做到的,创建一个这样的NotificationProvider.java

public class NotificationProvider {

public NotificationProvider() {
}

public void setNotification(Context context, String notificationTitle, String notificationMessage, int notificationRequestCode){
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.icon_mother_and_child_care)
                    .setContentTitle(notificationTitle)
                    .setColor(101)
                    .setContentText(notificationMessage);

    Intent intent = new Intent(context, DashboardActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, notificationRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}

}

并像这样调用setNotification()函数activity.class

public class MainActivity extends AppCompatActivity {

private NotificationProvider notificationProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notificationProvider = new NotificationProvider();
    notificationProvider.setNotification(this, "Title", "Message", 1);
}
于 2018-05-15T07:34:55.943 回答