2016-07-10 247 views
1

兩個服務之間的通信我在Android使用廣播接收器

M個新的,我想談談兩個服務,一個服務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

您是否在Manifest中註冊過接收器?你爲什麼不使用本地廣播? –

+0

哦,這是正確的,我忘了它,但接收機意圖過濾器,因爲我創建自定義意圖我有更新我的問題,我也會看看當地廣泛演員也。 –

回答

1

根據@Evgeniy Mishustin答案(感謝他) 的解決方案是在人加入服務ifest 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> 
+0

很高興我幫了忙 –