我尝试使用Broadcasts在两个服务之间发送一些数据。问题是:我的广播接收器类没有获取数据(没有从发送者那里获得任何数据),我不知道为什么。我将不胜感激任何帮助。感谢。
接收者 -
public class Background extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(ActivityRecognizedService.LOCAL_BROADCAST_EXTRA);
Log.e(TAG,message);
}
}
发件人 -
public class ActivityRecognizedService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
Intent intent = new Intent(LOCAL_BROADCAST_NAME);
intent.putExtra(LOCAL_BROADCAST_EXTRA, str.toString());
//LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
sendBroadcast(intent);
}
答案 0 :(得分:0)
您需要在onStartCommand
注册您的接收器。
像registerReceiver(new MyReceiver(),new IntentFilter(LOCAL_BROADCAST_NAME))