为什么我的服务没有返回数据?

时间:2011-08-02 18:01:18

标签: android accelerometer android-sensors

应用程序需要在后台监视加速度计,我试图将(工作)代码从Activity移动到服务中,但是我无法获取任何日志标记来指示main方法中的代码(sendUpdatesToUI) )不工作。

代码:

public class BroadcastService  extends Service {

private static final String TAG = "BroadcastTest SERVICE";
public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent";
public float xAccel;
public boolean shakeInitiated = false;
public float yAccel;
public float zAccel;
public float xPreviousAccel;
public float yPreviousAccel;
public float zPreviousAccel;
public boolean firstUpdate = true;
public final float shakeThreshold =3;

private final Handler handler = new Handler();
Intent intent;
int counter = 0;
public String XZnds;
public boolean zrun=true;

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "BroadcastTest SERVICE onCreate()");

    intent = new Intent(BROADCAST_ACTION);  
}

@Override
public void onStart(Intent intent, int startId) {
    Log.d(TAG, "BroadcastTest SERVICE onStart()");
    handler.removeCallbacks(sendUpdatesToUI);
    Thread thread = new Thread(sendUpdatesToUI);
    thread.start();
    handler.postDelayed(sendUpdatesToUI, 1); // 1 second

}

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {


        Log.d(TAG, "BroadcastTest SERVICE entered Runnable");
        //SensorManager mySensorManager;
        String Sanity = "BroadcastTest SERVICE SANITY CHECK";
        Log.d(TAG, Sanity);

        SensorEventListener mySensorEventListener = new SensorEventListener() {  
            public void onSensorChanged(SensorEvent se) {
                while (zrun==true){
                 Log.d(TAG, "BroadcastTest SERVICE onSensorEventListener() void");
                 /* we will fill this one later */
                    updateAccelParameters(se.values[0], se.values[1], se.values[2]);   // (1)
                    if ((!shakeInitiated) && isAccelerationChanged()) {                                      // (2) 
                        shakeInitiated = true; 
                        Log.d(TAG, "BroadcastTest SERVICE    shakeInitiated ");
                    } else if ((shakeInitiated) && isAccelerationChanged()) {                              // (3)
                        executeShakeAction();
                        Log.d(TAG, "BroadcastTest SERVICE Shake action");
                    } else if ((shakeInitiated) && (!isAccelerationChanged())) {                           // (4)
                        shakeInitiated = false;
                        Log.d(TAG, "BroadcastTest SERVICE Not Shaken ");
                        //notshaken();
                    }
                }
            }
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                /* can be ignored in this example */
                   }

            private void executeShakeAction() {
                XZnds="Shaken";

            }
            private void noshaken(){
                XZnds="NOT shaken!...";
            }
            private void updateAccelParameters(float xNewAccel, float yNewAccel,
                    float zNewAccel) {
                 Log.d(TAG, "BroadcastTest SERVICE updateAccelParameters()");
                        /* we have to suppress the first change of acceleration, it results from first values being initialized with 0 */
                if (firstUpdate) {  
                    xPreviousAccel = xNewAccel;
                    yPreviousAccel = yNewAccel;
                    zPreviousAccel = zNewAccel;
                    firstUpdate = false;
                } else {
                    xPreviousAccel = xAccel;
                    yPreviousAccel = yAccel;
                    zPreviousAccel = zAccel;
                }
                xAccel = xNewAccel;
                yAccel = yNewAccel;
                zAccel = zNewAccel;
            }

            /* If the values of acceleration have changed on at least two axises, we are probably in a shake motion */
            private boolean isAccelerationChanged() {
                float deltaX = Math.abs(xPreviousAccel - xAccel);
                float deltaY = Math.abs(yPreviousAccel - yAccel);
                float deltaZ = Math.abs(zPreviousAccel - zAccel);
                return (deltaX > shakeThreshold && deltaY > shakeThreshold)
                        || (deltaX > shakeThreshold && deltaZ > shakeThreshold)
                        || (deltaY > shakeThreshold && deltaZ > shakeThreshold);
            }

        }

        ;

        DisplayLoggingInfo();           
        handler.postDelayed(sendUpdatesToUI, 1); // 10 seconds
        // Log.d(TAG, "BroadcastTest onCreate()");
    }
};    



private void DisplayLoggingInfo() {
    Log.d(TAG, "BroadcastTest Service entered DisplayLoggingInfo");

    intent.putExtra("time", new Date().toLocaleString());
    intent.putExtra("counter", XZnds);
    Log.d(TAG, "BroadcastTest SERVICE nds the egg is="+XZnds);
    sendBroadcast(intent);
}



@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {       

    handler.removeCallbacks(sendUpdatesToUI);       
    super.onDestroy();
}       

}

我的活动(广播接收器)代码:

public class BroadcastTest extends Activity {
public static final String TAG = "BroadcastTest Activity";
private Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    intent = new Intent(this, BroadcastService.class);
    Log.d(TAG, "BroadcastTest Activity onCreate()");
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
         Log.d(TAG, "BroadcastTest Activity reciever()");
        updateUI(intent);       
    }
};    

@Override
public void onResume() {
    super.onResume();       
     Log.d(TAG, "BroadcastTest Activity onResume()");
    startService(intent);
    registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
}

@Override
public void onPause() {
     Log.d(TAG, "BroadcastTest Activity onPause()");
    super.onPause();
    unregisterReceiver(broadcastReceiver);
    stopService(intent);        
}   

private void updateUI(Intent intent) {
     Log.d(TAG, "BroadcastTest Activity updateUI");
    String counter = intent.getStringExtra("counter"); 
     Log.d(TAG, "BroadcastTest Activity data from service ="+counter);
    String time = intent.getStringExtra("time");


    TextView txtDateTime = (TextView) findViewById(R.id.txtDateTime);   
    TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
    txtDateTime.setText(time);
    txtCounter.setText(counter);
}

}

我的日志文件(来自aLogCat)显示:

BroadcastTest Activity reciever()
BroadcastTest Activity updateUI
BroadcastTest Activity data from service =null
exit dispatch OnReceive message,mRegistered=true mCurOrdered=false
BroadcastTest SERVICE entered Runnable
BroadcastTest Service entered DisplayLoggingInfo
BroadcastTest SERVICE nds the egg is=null

服务没有返回任何数据,我做错了什么?

2 个答案:

答案 0 :(得分:0)

我假设这两个方法都没有被调用,所以XZnds保持为null:

        private void executeShakeAction() {
            XZnds="Shaken";
        }
        private void noshaken(){
            XZnds="NOT shaken!...";
        }

在其中添加调试打印以进行检查。

答案 1 :(得分:0)

可以试试这个:

public class BroadcastService  extends Service, implements SensorEventListener {

我也看不到你在哪里注册和取消注册mySensorEventListener 在SensorManager

上阅读一点点