BroadcastReceiver内部活动未接收来自服务的广播

时间:2015-08-01 03:07:36

标签: android

我正在创建一个具有活动的应用程序和一个在后台执行某些加速计功能的intentservice。我尝试发送广播来更新我的UI从服务到活动。我在我的活动的onCreate()方法中创建了一个广播接收器

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);

我的服务如下:

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,似乎工作正常

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;
}

这是从

发送广播的地方
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");
}

在监控logcat中,广播已发送会显示,但收到的广播永远不会出现。无法做一些简单的事情,比如将某些东西从服务发送到某个活动,这让我无法做到。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

第一次经历这个时,我跟着这个:

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

尝试活动:

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

在服务中:

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

答案 1 :(得分:0)

想出来......

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

应该是

sendAcceleration = new Intent();