ä½è¤ã§ãã
ãããã¨ããã度ã
ãããã¨ããããã¾ãã
æããããµã¼ãã¹ã¯åãã¦ããã¨æãã®ã§ãã
確証ãããããã§ã¯ãªãã®ã§ã以ä¸ã®æµãã¨ã½ã¼ã¹ã®ä¸é¨ãè¼ãã¦ããã¾ãã
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 <
[email protected]> wrote:
> ä½è¤ãã
>
> ãããã¨ã§ãã
>
> Serviceã¯æ°¸ç¶çã«èµ·åãã¦ãããã¨ãä¿è¨¼ããã¦ãããã®ã§ã¯
> ããã¾ããã®ã§ãé »ç¹ã«æ®ºããã¾ãããã
> ä½è¤ãããåå¾ãããã¨ãã¦ããã¿ã¤ãã³ã°ã§ãµã¼ãã¹ãåãã¦ããªãå¯è½æ§ãé«ãã¨æãã¾ãã
>
> Serviceã®åèµ·åï¼ï¼ï¼ã®å¦çãå¿
è¦ã®æ§ãªæ°ããã¾ããã
> ãã£ã¨æèè
ã®æ¹ã®æå©ãã欲ããã¨ããã§ããã
>
> 2010å¹´4æ28æ¥14:20 å¯å·± ä½è¤ <
[email protected]>:
> >> > Activityå´ãããrequestLocationUpdatesã§GPSåå¾ãæå¹ã«ã(LocationManager.GPS_PROVIDERã--使ã£ã¦ãã¾ãã)