使用BroadcastReceiver在两个服务之间进行通信

时间:2016-07-10 10:13:26

标签: android android-service android-broadcastreceiver

我是关于android的新手

我想开始两个服务和一个服务sendbroadcast另一个将捕获这个广播,但即使我在此广播中注册另一个服务它也无法正常工作 这是我的代码,我做错了什么?

谢谢你的建议。

    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);
    }

}

SimpleService

public class SimpleService extends Service implements SensorEventListener {


    private String reading;
    private SensorManager mgr;
    private List<Sensor> 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 < 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);
    }
}

AnotherService

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);
        }
    }
}

清单

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

1 个答案:

答案 0 :(得分:1)

据@Evgeniy Mishustin回答(感谢他) 解决方案是在清单xml文件中添加服务,现在正在处理每个服务通信

        <service android:name=".SimpleService" ></service>
        <service android:name=".AnotherService"></service>
        <receiver android:name=".AnotherService$AnotherServiceReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="com.practice.SimpleService.MY_ACTION"></action>
            </intent-filter>
        </receiver>
    </application>