Communication Between Two Services using BroadcastReceiver
<p>I m new on android </p>

<p>I want to start two services and one service sendbroadcast another will catch this broadcast but it is not working even if I register another service in this broadcasting
here is my code am I doing something wrong ?</p>

<p>Thanks advice.</p>

<pre><code>    public class MainActivity extends Activity {

    Intent i;
    Intent i2;
    static final String LOG_TAG = "ServiceActivity";

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(LOG_TAG, "onCreate");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //Start service
        i = new Intent(this, com.example.user.sensorservicetest.SimpleService.class);
        i2= new Intent(this,com.example.user.sensorservicetest.AnotherService.class);
        Log.d(LOG_TAG, "onCreate/startService");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(LOG_TAG, "onResume/registering receiver");
        //Register BroadcastReceiver to receive accelerometer data from service

        startService(i);
        startService(i2);

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(LOG_TAG, "onPause/unregistering receiver");
        stopService(i);
        stopService(i2);


    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(LOG_TAG, "onStop");

        stopService(i);
        stopService(i2);
    }

}
</code></pre>

<p>SimpleService</p>

<pre><code>public class SimpleService extends Service implements SensorEventListener {


    private String reading;
    private SensorManager mgr;
    private List&lt;Sensor&gt; sensorList;
    static final String LOG_TAG = "SimpleService";
    Intent intent = new Intent("com.practice.SimpleService.MY_ACTION");
    final static String MY_ACTION = "com.practice.SimpleService.MY_ACTION";

    @Override
    //public void onStartCommand() {
    public void onCreate() {
        Log.d(LOG_TAG, "onStartCommand");
        mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorList = mgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
        for (Sensor sensor : sensorList) {
            mgr.registerListener(this, sensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        Log.d(LOG_TAG, "onDestroy");
        mgr.unregisterListener(this);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Log.d(LOG_TAG, "onSensorChanged");
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i &lt; event.values.length; i++) {
            builder.append("   [");
            builder.append(i);
            builder.append("] = ");
            builder.append(event.values[i]);
            builder.append("\n");
        }

        reading = builder.toString();

        //Send back reading to Activity
        intent.putExtra("measurement", reading);
        sendBroadcast(intent);
    }
}
</code></pre>

<p>AnotherService</p>

<pre><code>public class AnotherService extends Service {
    static final String TAG = "AnotherService";
    private AnotherServiceReceiver anotherServiceReceiver = new AnotherServiceReceiver();


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.practice.SimpleService.MY_ACTION");

        registerReceiver(anotherServiceReceiver, intentFilter);
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        if (anotherServiceReceiver != null)
            unregisterReceiver(anotherServiceReceiver);

        super.onDestroy();
    }

    public static class AnotherServiceReceiver extends BroadcastReceiver {
        static final String receiverTag = "AnotherServiceReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(receiverTag, "onReceive");
            String measurement = intent.getStringExtra("measurement");
            Log.d(receiverTag, "measurement - 2 : " + measurement);
        }
    }
}
</code></pre>

<p>Manifest</p>

<pre><code>  &lt;service android:name=".SimpleService" &gt;&lt;/service&gt;
        &lt;service android:name=".AnotherService"&gt;&lt;/service&gt;
        &lt;receiver android:name=".AnotherService$AnotherServiceReceiver" android:enabled="true"&gt;
            &lt;intent-filter&gt;
                &lt;action android:name="here is problem I think"
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
</code></pre>