An app needs to monitor the accelerometers in the background, and I have tried to move the (working) code from an Activity into a service, but I cannot get any log tags to indicate that the code inside the main methods (sendUpdatesToUI) is not working.
Code:
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();
}
}
My Activity (Broadcast reciever) code:
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 In开发者_JAVA技巧tentFilter(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);
}
}
My log file (from aLogCat) shows:
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
The service is not returning any data, what have i done wrong?
I assume none of those two method is getting called, so XZnds
stays null:
private void executeShakeAction() {
XZnds="Shaken";
}
private void noshaken(){
XZnds="NOT shaken!...";
}
Add debug prints inside of them to check it.
Might try this:
public class BroadcastService extends Service, implements SensorEventListener {
I also don't see where you register and unregister mySensorEventListener.
A little light reading on SensorManager
精彩评论