2011-08-02 23 views
0

アプリケーションはバックグラウンドで加速度計を監視する必要があります。アクティビティから(作業中の)コードをサービスに移動しようとしましたが、主なメソッド(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(); 
}  

}

My活動(ブロードキャストレシーバ)コード:(aLogCatから)

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

}

マイログファイルは示しています

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 

サービスでデータが返されていない、何が間違っていますか?

+0

サービスからアクティビティへデータを渡すためにブロードキャストを使用していますか?これらの放送を受信したコードを投稿することを確認できますか? –

+0

@fred、Kumarは指摘しているように、あなたの 'BroadcastReceiver'を見ることなく、問題がどこにあるのかを本当にデバッグすることはできません。 – Phil

答えて

0

私はこれら二つの方法のいずれも呼び出されないばかりされていると、そうXZnds滞在をnull:

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

はそれを確認するためにそれらの内部デバッグプリントを追加します。

0

はこれを試してみてください:あなたはmySensorEventListenerを登録し、登録解除どこ

public class BroadcastService extends Service, implements SensorEventListener { 

また、私は表示されません。
少し明るい読み値SensorManager

関連する問題