首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法从服务生成的广播接收器获取数据?

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

Stack Overflow用户
提问于 2019-06-10 00:32:19
回答 1查看 35关注 0票数 0

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

代码语言:javascript
复制
Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
intent.putExtra(EXTRA_LATITUDE, lat);
intent.putExtra(EXTRA_LONGITUDE, lng);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

在我的MainActivity1中,我通过以下代码获得了纬度和经度,该代码运行良好

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
 @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工作正常,可以设置纬度和经度文本视图。

MainActivity2

代码语言:javascript
复制
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()方法中的mlatitude处得到空指针异常。

请帮帮忙,告诉我我哪里做错了。

EN

回答 1

Stack Overflow用户

发布于 2019-06-10 13:16:02

在清理项目和InvalidateCache/Restart之后,我终于解决了这个问题。感到尴尬:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56516392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档