0
私は、ロケーションサービスを可能にし、サーバーにデータを送信するアンドロイドアプリを開発しました。私は、位置情報サービスの開始と停止のようなアプリを制御したいと思っています。サービスはアンドロイドで止まらない
AlarmManager
を実装して、一定時間後にサービスを再開しました。しかし、このサービスを停止するためにToggleButton
のチェックを外すと、機能しません。サービスは停止しません。サービスを停止した後も継続的にサーバにデータを送信して送信しています。
私のサービスのonDestroy()
メソッドと以下のon/off
チェックコードを追加しました。
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.v("STOP_SERVICE", "DONE");
Toast.makeText(getApplicationContext(), "Service Stopped", Toast.LENGTH_SHORT).show();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(listener);
}
コード
tracking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true){
AlarmRecurrer loc = new AlarmRecurrer(getApplicationContext());
loc.setRecurringAlarmSchedule(getApplicationContext());
}
if (isChecked == false){
Intent locationService = new Intent(getApplicationContext(), LocationService.class);
getApplicationContext().stopService(locationService);
}
}
}
を制御するオン/オフサービスは、誰も私にこの問題を解決するために可能な方法を教えてください。