Androidにはない場合は、インテントサービススティッキとセンサーの監視が停止します
-
14-11-2019 - |
質問
私のアプリケーションでは登録している条件を確認してから、このような活動を始めます。
Intent startIntent = new Intent(getApplicationContext(), EnableLocationProviderActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(startIntent);
.
その活動から、私はセンサーのためにいくつかのリスナーを登録するインテントサービスを始動します、それはスティッキーの意味として始まりますそれは明示的に止まるべきです。そのインテントサービスはセンサーを監視します。
私の問題は、私が最初の活動に戻ってきたときにセンサーはもう検出されていないことです(私はOnSensorChanged(データ表示の開始、そしてそれは停止します)。
私が明示的に停止しなかったならば、それが止まるのはなぜですか? さらに、私はテントサービスのondestroyが呼ばれることがありますが、再び、それが粘着性であるならばそれがどのように呼ばれていて、私は停止()に電話をかけず、他の方法では止まらなかったのでしょうか。
ありがとう! Guillermo。
編集
これは、携帯電話が眠りにつくのか、またはホームボタンが押されるかどうかにもかかわらず、これは常に実行されるべきであるべきであるべきである(私はバッテリーと他のすべてのものについて知っている)です。彼が望むときにアプリケーションを閉じるのはoportunityを持っています。
このようなmanactivityから呼び出されます。
.
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);
スタートグラウンドとして起動するが、通知バーにアイコンを入れてから、サービス内でondestroyが呼び出され、通知アイコンが消えます。
今すぐ必死です!これを手伝ってください!
ありがとう! Guillermo。
解決 2
OK、私は他の質問に返事をしました、そしてそれがAndroidのバグであると言った男がいます、私は彼の提案に従った。だから、誰も私に表示されないならば、それは私のコードの問題です、私はそれはバグになるでしょう。ありがとう!
他のヒント
サービスは必要に応じて開始され、順番に各意図を順番に処理します。 労働者スレッド、、それが働いて働いて停止する
も、同じドキュメントによると、
onStartCommand()
のonDestroy()
とIntentService
をオーバーライドすることは想定されていません。Service
の代わりにIntentService
を拡張する必要がある場合もあります。