无法从服务生成的广播接收器获取数据?

时间:2019-06-09 16:32:19

标签: android broadcastreceiver android-service android-service-binding

我已经定义了一个名为LocationService的服务,该服务通过LocalBroadcastManager发送意图,如下所示:

Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra(EXTRA_LATITUDE, lat);
intent.putExtra(EXTRA_LONGITUDE, lng);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

在我的MainActivity1中,以下代码可以很好地实现经纬度

LocalBroadcastManager.getInstance(this).registerReceiver(
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                 mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE));
                 mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));

             }
        }, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAS));

现在,我已经在另一个活动(例如MainActivity2)中启动了此服务,这是代码:

LocationService

public class LocationService extends Service implements LocationListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    public LocationService getService() {
        return LocationService.this;
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){ ... }

@Override
public IBinder onBind(Intent intent) {

    return mBinder;
}

@Override
public void onConnected(Bundle dataBundle) { ... }

 @Override
public void onLocationChanged(Location location) {
        sendMessageToUI(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));

    }
}

private void sendMessageToUI(String lat, String lng) {

    Log.d(TAG, "Sending info...");

    Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
    intent.putExtra(EXTRA_LATITUDE, lat);
    intent.putExtra(EXTRA_LONGITUDE, lng);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}


@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

MainActivity1

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

 LocalBroadcastManager.getInstance(this).registerReceiver(
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                Log.d(TAG, "onReceive:  called");
                mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE));
                mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));

 if (mlattitude != null && mlongitude != null) {
                    mLatlngTxtVu.setText("Latitude : " + mlattitude + "Longitude: " + mlongitude);
        }
      }
    }, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAS));
  

MainActivity1运行正常,并设置了纬度和经度textview。

MainActivity2

public class MainActivity2 extends AppCompatActivity {
        // Service related declaration
LocationService mLocationService;
boolean mBound = false;


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2_main);

   Intent serviceIntent = new Intent(this, LocationService.class);
    Log.d(TAG, "onCreate: intent started");
    startService(serviceIntent);

   LocalBroadcastManager.getInstance(this).registerReceiver(
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                Log.d(TAG, "onReceive:  called");
                mlattitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LATITUDE)); // Getting NullPointer Error here
                mlongitude = Double.parseDouble(intent.getStringExtra(LocationService.EXTRA_LONGITUDE));

                Log.d("latlng", "onReceive: " + mlongitude + mlattitude);


                if (mlattitude != null && mlongitude != null) {
                    mLatlngTextVu.setText("Latitude : " + mlattitude + "Longitude: " + mlongitude);


                }
            }
        }, new IntentFilter(LocationService.ACTION_LOCATION_BROADCAST));
}

我在MainActivity2的onRecieve()方法中遇到了空指针异常。

请帮助并告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

在清理项目并执行InvalidateCache / Restart之后,我终于解决了它。感到尴尬:)