我正在使用. _ _ Service
因为我Service
在登录后打电话给一个:
context.startService(new Intent(LoginActivity.this, CheckAutoSyncReceivingOrder.class));
context.startService(new Intent(LoginActivity.this, CheckAutoSyncSendingOrder.class));
我在上面都调用了一个计时器Service
CheckAutoSyncReceivingOrder 服务:
它每 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 连接可用时,它将自动再次呼叫。
主要是我在自动销毁活动服务调用时遇到问题。
有什么解决方案或方法可以改变同一件事的流程吗?
提前谢谢你。