【发布时间】:2016-01-09 10:28:30
【问题描述】:
我正在使用Service 管理从服务器到客户端和客户端到服务器的数据。因为我在登录后打电话给Service:
context.startService(new Intent(LoginActivity.this, CheckAutoSyncReceivingOrder.class));
context.startService(new Intent(LoginActivity.this, CheckAutoSyncSendingOrder.class));
我在Service上面都调用了一个计时器
CheckAutoSyncReceivingOrder 服务:
它在 Every 1 上调用另一个名为
ReceivingOrderService的服务 分钟从服务器获取更新数据。
public class CheckAutoSyncReceivingOrder extends Service {
Timer timer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "CheckAutoSyncReceivingOrder Binding Service...");
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
if (timer != null) {
timer.cancel();
Log.i(TAG, "RECEIVING OLD TIMER CANCELLED>>>");
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG, "<<<<<<<<< RECEIVING AUTO SYNC SERVICE <<<<<<<<");
if (InternetConnection.checkConnection(getApplicationContext())) {
if (getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
startService(new Intent(
CheckAutoSyncReceivingOrder.this,
ReceivingOrderService.class));
} else {
Log.d(TAG, "Connection not available");
}
}
}, 0, 60000); // 1000*60 = 60000 = 1 minutes
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if (timer != null)
timer.cancel();
Log.d(TAG, "Stopping Receiving...");
super.onDestroy();
}
}
CheckAutoSyncSendingOrder 服务:
它每 2.5 调用另一个名为
SendingOrderService的服务 分钟将更新的数据发送到服务器。
public class CheckAutoSyncSendingOrder extends Service {
Timer timer;
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
if (timer != null) {
timer.cancel();
Log.i(TAG, "OLD TIMER CANCELLED>>>");
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG, ">>>>>>>> SENDING AUTO SYNC SERVICE >>>>>>>>");
if (InternetConnection.checkConnection(getApplicationContext())) {
if (getDatabasePath(DatabaseHelper.DATABASE_NAME).exists())
startService(new Intent(CheckAutoSyncSendingOrder.this,
SendingOrderService.class));
} else {
Log.d(TAG, "connection not available");
}
}
}, 0, 150000); // 1000*60 = 60000 = 1 minutes * 2.5 = 2.5 =>Minutes
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if (timer != null)
timer.cancel();
Log.d(TAG, "Stopping Sending...");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
这两个 Activity 将一直运行到 Internet 关闭。当 Internet 连接可用时,它会自动再次调用。
主要是我遇到了问题在自动销毁活动服务调用时。
是否有任何解决方案或方法可以改变同一事物的流程?
提前谢谢你。
【问题讨论】:
-
它是
IntentServiceI have posted this question 的子类,但我认为我的问题在于流量,所以我再次发布。 -
我已经在我的last question中解释过了
-
我刚刚解决了错误日志的问题,其中我收到了
tokennull 的错误,但我仍然遇到调用破坏活动的服务问题。 -
我没有覆盖除
onCreate()之外的任何其他方法 -
@PratikButani 您是否使用过 IntentService 而不是 Service?如果您查看stackoverflow.com/questions/15524280/service-vs-intentservice,您会发现 Service 在后台运行,但它在应用程序的主线程上运行,因此如果主线程被破坏,服务将重新启动,因此您需要使用 IntentService 并覆盖 onStartCommand(Intent, int, int) ,将所有实现代码移至此方法并在该方法中返回 START_NOT_STICKY
标签: android android-activity android-service android-lifecycle android-timer