我一直在努力弄清楚如何将数据从服务类发送到mainactivity类。我现在正在使用广播接收器来做这件事。问题是它们没有在MainActivity类中被接收。
$scope.actionsArray = [{
functionName: $scope.addVideos('clip'),
name: "Add Video Clip"
}, {
functionName: $scope.addVideos('title'),
name: "Add Movie Title"
}];
发送数据。但是,当我尝试从MainActivity访问它时,它似乎并没有得到它。
onClick
有谁知道我做错了什么?
答案 0 :(得分:1)
首先,您应该为您的意图添加一个动作,假设位置,然后替换
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
sendBroadcast(intent);
与
intent.setAction("location");
intent.putExtra("Latitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
intent.putExtra("Longitude", mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
并替换
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
带
IntentFilter filter = new IntentFilter("location");
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, filter);
你应该在开始服务之前注册接收器,所以替换
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter());
}
与
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter());
startService(intent);
}
由于您已在onResume
注册,因此您可以删除
LocalBroadcastManager.getInstance(this).registerReceiver(
broadcastReceiver, new IntentFilter());
来自onCreate
方法。
答案 1 :(得分:1)
您需要在注册接收器时在intent过滤器中添加一些操作,并且在发送广播时,您需要将此操作设置为您的意图。