我的应用程序有一个后台服务,它使用MainActivity中的代码安排它:
Intent myIntent = new Intent(MainActivity.this , CheckUpdateService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); //set repeating every 24 hours
这是我的CheckUpdateService
:
public class CheckAppsUpdateService extends Service {
private GetJSONData update_notif_service;
public CheckAppsUpdateService() {
}
@Override
public IBinder onBind(Intent intent) {
//throw new UnsupportedOperationException("Not yet implemented");
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ConnectionStatusDetector csd = new ConnectionStatusDetector(this);
if (csd.isConnectingToInternet()) {
try {
// execute an asycntask here for getting Json data from server
}catch (Exception e){
Log.e("Update service error", "nothing");
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
if (update_notif_service != null && update_notif_service.getStatus() == AsyncTask.Status.RUNNING) {
update_notif_service.cancel(true);
}
}
}
一切都很完美,但是当我进入Setting->Apps->Running
标签1小时后,我看到我的服务在列表的第一位,大小为10MB到20MB(取决于设备的类型)。
和这不是一切。这项服务不断增长,以便在24小时后大小为:15MB到30MB(取决于设备的类型)!!!!
什么是问题?我该如何解决?我必须小心一些事吗?
提前谢谢。