2

我的应用程序中有一个 Activity 和一个 Service ,并且我正在向他们每个人发送广播。当我的活动接收从我的服务发送的广播时,我的服务不会接收从我的活动发送的广播。

这是我的一些代码:

活动

    @Override
public void onResume()
{
    Log.i(TAG, "in onResume");

    IntentFilter messageFilter = new IntentFilter(Constants.MESSAGE);
    messageReceiver receiver = new messageReceiver();
    registerReceiver(receiver, messageFilter);

    startService(new Intent(this, Server.class));

    super.onResume();
}

public class messageReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive (Context context, Intent intent)
    {
        Log.i(TAG, "in messageReceiver");
        serverStatus.setText(intent.getStringExtra(Constants.MESSAGE));
    }
}

@SuppressLint("ParserError")
public OnClickListener btnSendClicked = new OnClickListener() {

    public void onClick(View v) {
        Log.i(TAG, "btnSend clicked");

// This broadcast is NOT picked up by my Service
        Intent sendClicked = new Intent(Constants.SEND_CLICKED);
        sendBroadcast(sendClicked);
    }
};

服务

    @SuppressLint({ "ParserError", "ParserError", "ParserError" })
public class btnSendClickedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        // Do some stuff
    }
};

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i(TAG, "in onStartCommand");

    SERVERIP = getLocalIpAddress();

    IntentFilter sendClickedFilter = new IntentFilter(Constants.SEND_CLICKED);
    btnSendClickedReceiver receiver = new btnSendClickedReceiver();
    registerReceiver(receiver, sendClickedFilter);

    runServer();

    return START_STICKY;
}

@SuppressLint("ParserError")
public void runServer()
{
    try
    {
        if (SERVERIP != null)
        {
            Log.i(TAG, "inside runServer");

// This broadcast is picked up by my activity:
            Intent intent = new Intent(Constants.MESSAGE);
            intent.putExtra(Constants.MESSAGE, "Listening on IP: " + SERVERIP);
            sendBroadcast(intent);
            serverSocket = new ServerSocket(SERVERPORT);
            while (true)
            {
                client = serverSocket.accept();
                Log.i(TAG, "connected");

                Intent connectedIntent = new Intent(Constants.MESSAGE);
                connectedIntent.putExtra(Constants.MESSAGE, "Connected");
                sendBroadcast(connectedIntent);


            }
        }
        else
        {
            // no internet connection
        }
    } catch (Exception e)
    {
        //Error
        e.printStackTrace();
    }
}

显现

        <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="message"/>
            <action android:name="SendClicked"/>
        </intent-filter>
    </activity>

    <service android:name=".Server" android:process=":remote">
    </service>

几天来我一直在尝试调试它,但我不确定问题是什么。任何帮助表示赞赏。

4

2 回答 2

0

androidManifest.xml 文件中未创建广播接收器条目 请添加上述条目并尝试。

于 2012-07-08T02:13:12.980 回答
0

我遇到过同样的问题。我取出 :remote 说明符然后它起作用了。我不知道为什么会有所不同。而且我也从未在清单中指定接收者。

于 2015-09-23T19:24:35.113 回答