0

我在 TI 的 OMAP4460 处理器上使用 Android 4.2。我的目标是,在启动后立即启动一项服务,以侦听多播套接字上的 SSDP 查询。我可以这样做,但是当我去创建时,MulticastSocket我得到了一个例外。例外来自底层datagramSocket。以下是一些代码片段:

<receiver          android:name="com.example.reciever.StartServicesAtBootReceiver" android:enabled="true" android:exported="false" android:label="StartServicesAtBootRemoteReceiver" >
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />

       <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</receiver>

我的onRecieve()样子是这样的:

public void onReceive(Context context, Intent intent) {

    Intent ServiceIntent = new Intent(context,
            myService.class);
    ComponentName serviceStarted = context
            .startService(ServiceIntent);
    if (serviceStarted != null)

}

最后,服务片段:

public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        if (null == ssdpServer) {
            ssdpServer = new UDPListenThread();
            ssdpServer.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

public class UDPListenThread extends Thread {

    protected MulticastSocket socket = null;

    public UDPListenThread() throws IOException {
        this("UDPListenThread");
    }

    public UDPListenThread(String name) throws IOException {
        super(name);
    }

    @Override
    public void run() {

            Socket mySocket = new Socket();
            socket = new MulticastSocket(SSDP_PORT);
            // socket.setLoopbackMode(false);
            socket.joinGroup(InetAddress.getByName(SSDP_ADDRESS));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

等等

注意:Socket确实已分配(此处未使用,因此不确定它是否有效)。另外,如果我尝试:

ConnectivityManager cm = (ConnectivityManager) getBaseContext()
    .getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();

从服务或接收方。activeNetwork回来了null。即使我activeNetwork像这样等待(来自服务):

        NetworkInfo activeNetwork = null;
        while (null == activeNetwork) {
            ConnectivityManager cm = (ConnectivityManager) getBaseContext()
                    .getApplicationContext().getSystemService(
                            Context.CONNECTIVITY_SERVICE);

            activeNetwork = cm.getActiveNetworkInfo();
            Log.d(this.getClass().getSimpleName(), "activeNetwork "
                    + activeNetwork);

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

即使我能够打开浏览器并从 UI 搜索,它仍然为空。

跟进:原来这是一个糟糕的内核构建。我猜你建在垃圾堆上,你会得到一个摇摇欲坠的结构。:)

4

1 回答 1

0

您的应用程序是否需要所有适当的权限?

我想你可能会错过ACCESS_NETWORK_STATECHANGE_WIFI_MULTICAST_STATE

于 2013-08-22T01:40:40.410 回答