アプリケーションonCreateで、いくつかの条件を確認してから、次のようなアクティビティを開始します。
Intent startIntent = new Intent(getApplicationContext(), EnableLocationProviderActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(startIntent);
そのアクティビティから、センサーのリスナーを登録するIntentServiceを開始します。これは、STICKYとして開始されます。つまり、明示的に停止する必要があります。そのIntentServiceはセンサーを監視します。
私の問題は、最初のアクティビティに戻ると、センサーがセンサーを検出しなくなったことです(Log.vをonSensorChangedに入れました(データの表示を開始してから停止します)。
明示的に停止しなかった場合、なぜ停止するのでしょうか。さらに、IntentServiceのOnDestroyが呼び出されることもありますが、これも、STICKYで、stopself()を呼び出さず、他の方法で停止しなかった場合、どのように呼び出すことができますか?
ありがとう!ギレルモ。
編集
これはIntentServiceのコードです(携帯電話がスリープ状態になったり、ホームボタンが押されたりしても、常に実行されている必要があります(バッテリーやその他すべてについて知っているので、ユーザーはこれについて警告され、機会があります)必要なときにアプリケーションを閉じます。
サービスは、次のようにMainActivityから呼び出されます。
Intent startIntent = new Intent(GdpTesisApplication.getInstance().getApplicationContext(), SensingService.class);
startService(startIntent);
そして、サービスコードはこれです:
public class SensingService extends IntentService implements SensorEventListener {
private float[] mAccelerationValues;
private SensorManager mSensorManager = null;
String sensorType = "";
public SensingService(String name) {
super(name);
setIntentRedelivery(true);
}
public SensingService() {
super("SensingService");
setIntentRedelivery(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(ApplicationName,"SensingService.onStartCommand");
super.onStartCommand(intent, flags, startId); // If this is not written then onHandleIntent is not called.
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Log.v(ApplicationName, "SensingService.onCreate");
initialize();
}
private void initialize() {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // This must be in onCreate since it needs the Context to be created.
mAccelerationValues = new float[3];
Log.v(ApplicationName, "Opening Location Service from Sensing Service");
LocationService myLocation = new LocationService();
myLocation.getLocation(this, locationResult);
}
@Override
public void onDestroy() {
Log.v(ApplicationName, "SensingService.onDestroy");
super.onDestroy();
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
}
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(ApplicationName, "SensingService.onHandleIntent");
if (mSensorManager != null) {
registerListeners();
}
}
public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(final Location location) {
if (location != null) {
Log.v(ApplicationName, "Location != null : (" + location.getLatitude() + "," + location.getLongitude() + ")");
} else {
Log.v(ApplicationName, "Location == null : (0,0)");
}
}
};
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent currentEvent) {
if (currentEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
return;
}
synchronized (this) {
float[] accelVals = null;
float totalForce = 0.0f;
int sensor = currentEvent.sensor.getType();
System.arraycopy(currentEvent.values, 0, mAccelerationValues, 0, 3); // We use System.arraycopy because of this:
switch (sensor) {
case Sensor.TYPE_ACCELEROMETER:
sensorType = "Accelerometer";
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
sensorType = "LinearAcceleration";
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues) + 1;
break;
case Sensor.TYPE_GRAVITY:
totalForce = SensorsHelpers.getTotalForceInGs(mAccelerationValues);
sensorType = "Gravity";
break;
}
Log.v(ApplicationName,DateHelper.GetUTCdatetimeFromDate(new Date()) + " - from sensingService");
}
}
private void registerListeners() {
Log.v(ApplicationName, "Registering sensors listeners");
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
}
}
更新2
これをメソッドonCreateに追加しました。
int NOTIFICATION_ID = 1;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
Notification notification = new Notification(R.drawable.ic_dialog_info, "Running in the Foregound", System.currentTimeMillis());
notification.setLatestEventInfo(this, "Title", "Text", pi);
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);
startForgroundとして開始しますが、アイコンを通知バーに配置すると、サービスでonDestroyが呼び出され、通知アイコンが消えます。
今必死です!これで助けてください!
ありがとう!ギレルモ。