简体   繁体   中英

BroadcastReceiver inside activity not receiving broadcasts from service

I'm making an application that has an activity and an intentservice that does some accelerometer stuff in the background. I try to send a broadcast to update my UI from the service to the activity. I created a broadcast receiver inside the onCreate() method of my activity

public class MainActivity extends Activity {


BroadcastReceiver receiver;

public static String TAG = "vic.zerotosixty";

@Override
protected void onCreate(Bundle savedInstanceState) {


    intentFilter = new IntentFilter();
    intentFilter.addAction(SpeedService.ACTION_UPDATE_ACCELERATION);

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG,"Broadcast Received");
            Bundle bundle = intent.getExtras();
            double acceleration = (double) bundle.get("acceleration");
            speedText.setText((3.6*acceleration) + " Km/h");                    //Update text field (3.6 is a unit conversion factor)
        }
    };

    registerReceiver(receiver, intentFilter);

My service looks like this:

public class SpeedService extends IntentService implements SensorEventListener {

public final static int METHOD_INITIALIZE = 0;
public final static int METHOD_STOP = 1;
public final static String ACTION_UPDATE_ACCELERATION = "update_acceleration";
public final static String TAG = "vic.zerotosixty";

int initialized = 0;
double x,y,z,a;

//LocalBroadcastManager broadcast;
Intent sendAcceleration;
Sensor accel;
SensorManager sm;

onHandleIntent, seems to be working fine

protected void onHandleIntent(Intent intent) {
    Bundle bundle = intent.getExtras();
    int method = (int) bundle.get("method");
    switch(method) {
        case METHOD_INITIALIZE:
            if (initialized == 0) {initialize();}
            break;
        default:
            break;
    }
}

private void initialize()         {
    Log.d(TAG, "initialized");
    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    accel = sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    sm.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
    sendAcceleration = new Intent(this,MainActivity.class);
    sendAcceleration.setAction(ACTION_UPDATE_ACCELERATION);
    //broadcast = BroadcastManager.getInstance(this);

    initialized = 1;
}

Here is where the broadcast is sent from

public void onSensorChanged(SensorEvent event) {
    x = event.values[0];
    y = event.values[1];
    z = event.values[2];
    a = Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2));

    sendAcceleration.putExtra("acceleration", a);
    sendBroadcast(sendAcceleration);
    Log.d(TAG,"broadcast sent");
}

From monitoring logcat, the broadcast sent shows up, but the broadcast received never does. Not being able to do something as simple as sending some stuff from a service to an activity is driving me up the wall. Any ideas?

When going through this for the first time I followed this:

https://developer.android.com/training/run-background-service/report-status.html

Try in the activity:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);

In the service:

LocalBroadcastManager.getInstance(this).sendBroadcast(sendAcceleration);

Figured it out...

sendAcceleration = new Intent(this,MainActivity.class);

should have been

sendAcceleration = new Intent();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM