0
私のメインフラグメント内のTextViewのテキストを現在のユーザの位置に設定したいと思います。この場所は、FusedLocationApiのサービスで宣言されています。サービスが動作していて、トーストメッセージで位置情報が更新されています。 この場所を設定しようとしましたが、NullPointerExを受信しました。デバッグ後、私はBroadcastReceiverの部分が決して呼び出されないのを見ました。 以下、私が使用するコードを入れます。
MainFragment.java(fusedLocationApiコード除く)MyService.javaのBroadcastReceiverのフラグメントのTextViewを更新します。
@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);
...}
一部
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);
}
HTTPではなくEventBusを使用することを検討して:// stackoverflowの。 com/questions/36525508/android-services-and-activities間のコミュニケーション、私の答えをチェックする –