我想在主片段中使用当前用户位置设置TextView文本。这个位置在具有FusedLocationApi的服务中被占用。服务正在运行,我可以在Toast消息中看到位置更新。
我尝试设置这个位置,但我收到了NullPointerEx。调试之后,我看到与BroadcastReceiver的部分永远不会调用。
下面我把我使用的代码。
MainFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIntent = new Intent(getContext(), MyService.class);
getActivity().startService(mIntent);
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(MyService.BROADCAST_ACTION));
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
updateUI(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
};
private void updateUI(Intent intent) throws IOException {
latitudeString = intent.getStringExtra("latitude");
longitudeString = intent.getStringExtra("longitude");
myTime = intent.getStringExtra("time");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
View view = inflater.inflate(R.layout.fragment_main, null);
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
saveAdress = (Button) view.findViewById(R.id.buttonSaveAdress);
getBack = (Button) view.findViewById(R.id.buttonGetBack);
latitude = Double.parseDouble(latitudeString); //Null pointer here from latitudeString
longitude = Double.parseDouble(longitudeString);
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
googleMap.setMyLocationEnabled(true);
...}
MyService.java的一部分(fusionLocationApi代码除外)
private static final String TAG2 = "MyService";
public static final String BROADCAST_ACTION = "package.where.is.myService.displayevent";
private final Handler handler = new Handler();
Intent intent;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("LOC", "Service init...");
isEnded = false;
mRequestingLocationUpdates = false;
mLastUpdateTime = "";
buildGoogleApiClient();
if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
startLocationUpdates();
}
return Service.START_REDELIVER_INTENT;
}
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
@Override
public void onDestroy() {
super.onDestroy();
stopLocationUpdates();
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 5000); // 5 seconds
}
};
private void DisplayLoggingInfo() {
Log.d(TAG2, "entered DisplayLoggingInfo");
intent.putExtra("latitude", mCurrentLocation.getLatitude());
intent.putExtra("longitude",mCurrentLocation.getLongitude());
intent.putExtra("time", mCurrentLocation.getTime());
Log.d(TAG2, String.valueOf(mCurrentLocation.getLatitude()));
//intent.putExtra("counter", String.valueOf(++counter));
sendBroadcast(intent);
}
答案 0 :(得分:1)
BroadcastReceiver不是最好的选择 - 请考虑使用EventBus