片段BroadCast接收器无法正常工作

时间:2017-11-13 19:38:52

标签: android android-fragments service broadcastreceiver fragment

我正在创建一个应用程序,我需要从片段上的服务获取不断的位置更新,问题是片段没有获得更新,我不确定是什么问题,这是服务:

public class GPService extends Service
{
 private LocationManager locMan;
 private Boolean locationChanged;
 private Handler handler = new Handler();
 static final int REQUEST_LOCATION = 1;

public static Location curLocation;
public static boolean isService = true;
public LocalBroadcastManager broadcast;


LocationListener gpsListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        if (curLocation == null) {
            curLocation = location;
            locationChanged = true;
            Intent intent= new Intent("GPSLocationUpdates");
            intent.putExtra("latitud",curLocation.getLatitude());
            intent.putExtra("latitud",curLocation.getLongitude());
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

        }else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){
            locationChanged = false;
            return;
        }else
            locationChanged = true;

        curLocation = location;

        if (locationChanged)
            locMan.removeUpdates(gpsListener);

    }
    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status,Bundle extras) {

    }

};

@Override
public void onCreate() {
    super.onCreate();

    curLocation = getBestLocation();

    if (curLocation == null)
        Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show();
    else{
        //Toast.makeText(getBaseContext(), curLocation.toString(), Toast.LENGTH_LONG).show();
    }

    isService =  true;
    broadcast= LocalBroadcastManager.getInstance(this);

}
final String TAG="LocationService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onStart(Intent i, int startId){
    handler.postDelayed(GpsFinder,1);
}

@Override
public void onDestroy() {
    handler.removeCallbacks(GpsFinder);
    handler = null;
    Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show();
    isService = false;
}

public IBinder onBind(Intent arg0) {
    return null;
}

public Runnable GpsFinder = new Runnable(){
    public void run(){

        Location tempLoc = getBestLocation();
        if(tempLoc!=null)
            curLocation = tempLoc;
        tempLoc = null;
        handler.postDelayed(GpsFinder,1000);
    }
};

private Location getBestLocation() {
    Location gpslocation = null;
    Location networkLocation = null;

    if(locMan==null){
        locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE);
    }
    try {
        if ( ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {

            //ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        }
        else {
            if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){


                locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);
                gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Intent intent= new Intent("GPSLocationUpdates");
                intent.putExtra("latitud",gpslocation.getLatitude());
                intent.putExtra("longitud",gpslocation.getLongitude());
                Log.wtf("COORDENATES TO SEND",gpslocation.getLatitude()+" "+gpslocation.getLongitude());
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

            }
            if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener);
                networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                Intent intent= new Intent("GPSLocationUpdates");
                Log.wtf("LAS COORDENADAS a enviar SON",networkLocation.getLatitude()+" "+networkLocation.getLongitude());

                intent.putExtra("latitud",networkLocation.getLatitude());
                intent.putExtra("longitud",networkLocation.getLongitude());
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
            }
        }

该服务在此活动中启动

 startService(new Intent(this, GPService.class));

在片段中,这是我创建它并注册它的方式

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    receiver= new GPSReceiver();
    //this.getActivity().registerReceiver(receiver, new IntentFilter());

    db=Database.getInstance(this.getContext());


}
@Override
public void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter("GPSLocationUpdates");
    getActivity().registerReceiver(receiver,filter);
}

@Override
public void onPause() {
    getActivity().unregisterReceiver(receiver);
    super.onPause();
}

最后这是Fragment中的接收者,我无法从服务中获取信息,任何想法?我不知道自己做错了什么。

private class GPSReceiver extends BroadcastReceiver {


    private LatLng changed;

    public GPSReceiver()
    {
        changed= new LatLng(0,0);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.wtf("RECIEVE", "IT IS IN BROADCAST");
        if (intent.getAction().equals("GPSLocationUpdates"))
        {
            Log.wtf("RECIEVE1", "INTENT ARRIVES");
            double lat= intent.getDoubleExtra("latitud",1);
            double longi= intent.getDoubleExtra("longitud",1);
            changed= new LatLng(lat,longi);

        }
       // String text = intent.getStringExtra("position");

    }
    public LatLng getChanged()
    {
        return changed;
    }
}

该服务似乎工作正常,我可以看到控制台中的坐标正在发送。

1 个答案:

答案 0 :(得分:1)

您没有将BroadcastReceiver注册到LocalBroadcastManager。您已将其注册到全球广播管理员,但在本地发送。

变化:

@Override
public void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter("GPSLocationUpdates");
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver,filter);
}

@Override
public void onPause() {
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
    super.onPause();
}