佐藤です。
いそもとさん、度々ありがとうございます。
恐らく、サービスは動いていると思うのです。
確証があるわけではないので、以下の流れとソースの一部を載せておきます。
Activity側で、下記ソースのstartService()を呼び出して
3つのサービスを起動し、requestLocationUpdatesでGPS更新を取得します。
各々の処理自体はできているのですが(Serviceが動いている状態)
GPSだけ更新できていないという状態です。
また、GPS起動中に下記ソースのstopService()を呼び出して
3つのサービスを停止して、removeUpdatesでGPS更新を止める。
この場合は、Serviceは3つとも停止されるのですが、
GPS更新が生きてしまっています。(端末上のGPSマークが消えない)
処理の流れとしては、
1.Activityでボタン操作を行い、startService()が実行されることにより
GPS取得を有効にし、Serviceを有効にし、Activityをfinish()して画面を破棄します。
2.Serviceがバックグラウンドで処理を行います。
3.アイコンから再びアプリを起動すると、再びActivityが表示されます。
4.ここでボタン操作を行うと、stopService()が処理されて今まで動いていたServiceが停止されます。
現在の問題点として、
2のServiceが処理を行っている間に、不定期にGPS更新が止まってしまう。
4のstopServiceを呼び出した際に、GPS更新が生きてしまっている。(端末上のGPSマークが消えない)
以下ソースです。
BaseActivity.java
(Activityの親クラスを独自で定義したクラスです)
protected void startService() {
//設定マスタ読み出し
SettingsDto settingsDto = mSettingsDto;
//定期通知メールフラグがONであれば定期メール配信サービスを起動する。
if
(CodeUtil.COMMON_FLAG_ON.equals(settingsDto.getRegularMailFlg()) ==
true) {
//メール配信サービス起動
PendingIntent regularMailIntent = PendingIntent.getService(this,
0,
new Intent(getApplicationContext(),
RegularMailService.class), 0);
AlarmManager regularAlarm = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
// 次回起動までの時間
int intervalRegularTime = REGULAR_MAIL_CYCLE_TIME_UNIT
* Integer.valueOf(settingsDto.getRegularMailCycle());
// 設定値の定期メール配信で定義したタイミングでサービス呼び出し
regularAlarm.setRepeating(AlarmManager.RTC,
System.currentTimeMillis()
+ intervalRegularTime, intervalRegularTime,
regularMailIntent);
}
//着信通知メールフラグがOnであれば着信通知配信サービスを起動する。
if(CodeUtil.COMMON_FLAG_ON.equals(settingsDto.getTelResponseFlg()))
{
//通話状態取得サービス起動
Intent telIntent = new
Intent(getApplicationContext(),TelephoneService.class);
telIntent.putExtra("type", "start");
super.startService(telIntent);
}
//GPSログフラグがOnであればGPS取得サービスを起動する。
if(CodeUtil.COMMON_FLAG_ON.equals(settingsDto.getGpsResponseFlg()))
{
//GPSログ取得サービス起動
PendingIntent mapIntent = PendingIntent.getService(this, 0,
new Intent(getApplicationContext(), MapService.class), 0);
AlarmManager gpsLogAlarm = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
// 次回起動までの時間
int intervalMapTime = MAP_CYCLE_TIME_UNIT
* Integer.valueOf(settingsDto.getGpsLogPreservationCycle());
// 設定値のGPSログ取得間隔で定義したタイミングでサービス呼び出し
gpsLogAlarm.setRepeating(AlarmManager.RTC,
System.currentTimeMillis()
+ intervalMapTime, intervalMapTime, mapIntent);
}
//参照を消す
settingsDto = null;
//------------------------------
//サービスマスタの起動フラグをON
//------------------------------
// Dtoから読み出し
ServiceDto serviceDto = mServiceDto;
//起動フラグをONに更新
serviceDto.setStartFlg(CodeUtil.COMMON_FLAG_ON);
//サービスマスタへ更新
sServiceDao.update(serviceDto);
//メンバDtoを最新状態に更新
mServiceDto = serviceDto;
//参照を消す
serviceDto = null;
//------------------------------
//GPS更新を有効にする
//------------------------------
//ロケーションマネージャを取得。
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//GPS更新を有効にする。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000,0,BaseActivity.this);
return;
}
protected void stopService() {
//------------------------------
//定期メール配信サービス停止処理
//------------------------------
PendingIntent regularMailIntent = PendingIntent.getService(this,
0,
new Intent(getApplicationContext(), RegularMailService.class),
0);
AlarmManager am = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
am.cancel(regularMailIntent);
//------------------------------
//通話状態取得サービス停止処理
//------------------------------
Intent telIntent = new Intent(getApplicationContext(),
TelephoneService.class);
telIntent.putExtra("type", "stop");
super.stopService(telIntent);
//------------------------------
//GPS取得サービス停止処理
//------------------------------
PendingIntent mapIntent = PendingIntent.getService(this, 0,
new Intent(getApplicationContext(), MapService.class), 0);
AlarmManager am2 = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
am2.cancel(mapIntent);
//------------------------------
//サービスマスタの起動フラグをOFF
//------------------------------
// Dtoから読み出し
ServiceDto serviceDto = mServiceDto;
//起動フラグをONに更新
serviceDto.setStartFlg(CodeUtil.COMMON_FLAG_OFF);
//サービスマスタへ更新
sServiceDao.update(serviceDto);
//メンバDtoを最新状態に更新
mServiceDto = serviceDto;
//------------------------------
//GPS更新を無効にする
//------------------------------
//ロケーションマネージャを取得。
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//GPS更新を無効にする。
locationManager.removeUpdates(BaseActivity.this);
return;
}
On 4月28日, 午後4:23, Satoru Isomoto <
isomoto.sat...@gmail.com> wrote:
> 佐藤さん
>
> いそもとです。
>
> Serviceは永続的に起動していることが保証されているものでは
> ありませんので、頻繁に殺されます。。。
> 佐藤さんが取得しようとしているタイミングでサービスが動いていない可能性が高いと思います。
>
> Serviceの再起動(?)の処理が必要の様な気がしますが、
> もっと有識者の方の手助けが欲しいところですね。
>
> 2010年4月28日14:20 寛己 佐藤 <
p10021.c...@gmail.com>:
> >> > Activity側から、requestLocationUpdatesでGPS取得を有効にし(LocationManager.GPS_PROVIDERを--使っています。)