2016-12-26 4 views
0

私はHuman Activity Recognition androidアプリケーションを開発していて、座っている(静的な)時間と活動している時間の両方を測定する必要があります。最初は、sharedPreferencesに長い変数を格納し、5秒ごとに増分します(5秒ごとにユーザーのアクティビティを分類しています)に5000(ミリ秒で5秒)を加算します。しかし、今私がサービスを開始したとき(私はクラサリゼーションを行いSharedPreferencesに格納するバックグラウンドサービスを使用しています)、後で、2時間後にsharedPreferencesと実際の時間通過した時間は著しく異なる。たとえば、私は午後12時にはサービスを開始し、午後2時にチェックします。共有設定の値は、40分(勤務時間が混雑している場合 - 値/ 1000/60分)のような値になります。私は上記の方法で時間を測定する方法に関するアイディアを感謝します。前もって感謝します!Androidで時間を測定する方法は?

P.S:私のサービスは、次のようになります。

public class MyService extends Service implements SensorEventListener { 
public static final String COUNTER_KEY = "counterKey3"; 
public int counter = 0; 
private static final long WINDOW_LENGTH = 5000; 
private long windowBegTime = -1; 
private SensorManager mSensorManager; 
private Sensor accSensor; 
private ArrayList<Double> xValues = new ArrayList<>(); 
private ArrayList<Double> yValues = new ArrayList<>(); 
private ArrayList<Double> zValues = new ArrayList<>(); 

private SharedPreferences mSharedPreferences; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_GAME); 
    return START_STICKY; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mSharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE); 
    counter = mSharedPreferences.getInt(COUNTER_KEY, 0); 

} 


@Override 
public void onSensorChanged(SensorEvent sensorEvent) { 
    xValues.add((double) sensorEvent.values[0]); 
    yValues.add((double) sensorEvent.values[1]); 
    zValues.add((double) sensorEvent.values[2]); 

    if (SystemClock.elapsedRealtime() - windowBegTime > WINDOW_LENGTH) { 
     if (windowBegTime > 0) { 
      mSharedPreferences.edit().putInt(COUNTER_KEY, mSharedPreferences.getInt(COUNTER_KEY, 0) + 5).apply(); 
      Log.i("MyService", "WindowTimeIssue! " + mSharedPreferences.getInt(COUNTER_KEY, 0)); 
       // DETECT ACTIVITY - store it in a db 

     } 

     windowBegTime = SystemClock.elapsedRealtime(); 
    } 
} 


} 

答えて

1

あなたはあなたのサービスフォアグラウンドサービスにする必要があります。バックグラウンドサービスは、リソースが必要なときにOSによって強制終了されます。

Intent intent = new Intent(this, MyActivityApp.class); 
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

    builder.setSmallIcon(Resource.Drawable.my_icon); 
    builder.setTicker("My Activity App"); 
    builder.setContentIntent(pi); 
    builder.setOngoing(true); 
    builder.setOnlyAlertOnce(true); 

    Notification notification = builder.build(); 
    startForeground(SERVICE_NOTIFICATION_ID, notification); 

また、あなたが行動認識のAPI(https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi

+0

になっているはずですあなたの答えをありがとう!私は間違いなく私のサービスをforegorundに変更することを検討します。 Activity Recognition APIについて。私のアプリケーションは活動的ではなく、非定型の人を認識するように設計されているので、私はそれを使用していません。だから私はアンドロイドのために自分自身の活動認識システムを使っているのです。 –

関連する問題