不运行BroadcastReceiver代码

时间:2017-04-10 10:17:19

标签: java android broadcastreceiver

我在数据库中有数字。

如果此号码等于。

,我想列出收到的短信

我的问题是我的最后一部分代码没有运行。 (结束后如果)。

请指导我。谢谢

incomingSms:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.command.RemoveInfo;

public class AdvisorySupportConnectionAdvisoryTopic {

    public static void main(String[] args) throws JMSException {
        Connection conn = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory("auto://localhost:5671");
            conn = cf.createConnection("admin", "admin");
            ActiveMQSession session = (ActiveMQSession) conn.createSession(false,
                    ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
            conn.start();
            Destination advisoryDestination = org.apache.activemq.advisory.AdvisorySupport.getConnectionAdvisoryTopic();
            MessageConsumer consumer = session.createConsumer(advisoryDestination);
            consumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message msg) {
                    if (msg instanceof ActiveMQMessage) {
                        try {
                            ActiveMQMessage aMsg = (ActiveMQMessage) msg;
                            if (aMsg.getDataStructure() instanceof ConnectionInfo) {
                                // Connection start
                                ConnectionInfo connectionInfo = (ConnectionInfo) aMsg.getDataStructure();
                                System.out.println(connectionInfo);
                            } else if (aMsg.getDataStructure() instanceof RemoveInfo) {
                                // Connection stop
                                RemoveInfo removeInfo = (RemoveInfo) aMsg.getDataStructure();
                                System.out.println(removeInfo);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您是否在清单文件中添加了接收器类,如果您不添加此代码,则接收器类无法正常工作!请检查一下。

<receiver
        android:name=".IncomingSms"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>