繁体   English   中英

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

[英]BroadcastReceiver inside activity not receiving broadcasts from service

我正在制作一个具有活动和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");
}

从监视日志中可以看到发送广播 ,但从未收到 无法完成像从服务向活动中发送某些内容这样简单的事情,这使我望而却步。 有任何想法吗?

第一次进行此操作时,我遵循以下步骤:

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

尝试活动:

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

在服务中:

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

弄清楚了...

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

本来应该

sendAcceleration = new Intent();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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