gpt4 book ai didi

android - 带有计时器的服务调用另一个服务

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:52 25 4
gpt4 key购买 nike

我正在使用服务管理从服务器到客户端客户端到服务器的数据。因为我在登录后调用一个 Service:

context.startService(new Intent(LoginActivity.this, CheckAutoSyncReceivingOrder.class));
context.startService(new Intent(LoginActivity.this, CheckAutoSyncSendingOrder.class));

我在 Service 上面调用了一个计时器

CheckAutoSyncReceivingOrder 服务:

Its calling another service named ReceivingOrderService on Every 1 minute to get updated data from server.

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 服务:

Its calling another service named SendingOrderService on Every 2.5 minute to send updated data to server.

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 连接可用时,它会自动再次调用。

主要是我遇到了问题在自动销毁 Activity 服务调用时

是否有任何解决方案或方法可以改变同一事物的流程?

提前致谢。

最佳答案

你必须使用 IntentService而不是 Service .

  • Service在后台运行,但在应用程序的主线程上运行。
  • IntentService在单独的工作线程上运行。

如果运行您的服务的进程被终止,Android 系统将自动重启它,这是默认行为。

因此,如果 Activity 的主线程被破坏,服务将重新启动,但如果您使用 IntentService并使用 START_NOT_STICKY然后它不会重新启动您的服务。

此行为由 onStartCommand() 的返回值定义在您的服务实现中。常量 START_NOT_STICKY告诉 Android 如果在进程被“杀死”时正在运行,则不要重新启动该服务。

您需要覆盖方法 onStartCommand()在您的服务类别中,将所有代码从 onStart() 中移出onStartCommand() 的方法方法。

根据 Android 文档:

For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them

onStart()每次重新启动服务时都会调用方法,但 onStartCommand()如果返回 START_NON_STICKY,方法将不会被调用.

有关IntentService 之间差异的更多详细信息和 Service .你可以查看this .

希望对你有帮助

关于android - 带有计时器的服务调用另一个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com