2012-07-08 96 views
2

我在我的應用程序中有一個活動和一個服務,我從另一個發送廣播給他們每個人。當我的活動接收到我的服務發送的廣播時,我的服務不會接收從我的活動發送的廣播。BroadcastReceiver沒有從服務內部接收意圖

下面是我的一些代碼:

活動

@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> 

我一直在試圖調試這幾天,但我不知道是什麼問題在於。任何幫助表示讚賞。

+0

您沒有在清單中聲明的​​接收器。 – FoamyGuy 2012-07-08 01:52:34

回答

0

BroadCast接收器條目尚未在androidManifest.xml文件中製作 請添加上面的條目並嘗試。

+1

我註冊我的接收器動態,所以,據我瞭解,我不需要註冊它在清單。如果是這種情況,我的接收器都不會起作用,因爲我沒有在清單中註冊其中的任何一個。 – user969540 2012-07-08 17:27:00

+0

@ user969540任何更新! – 2015-04-02 10:32:37

0

我有同樣的問題。我拿出:遠程說明符,然後它工作。我不知道爲什麼造成這種差異。而且我從未在清單中指定接收者。