gpt4 book ai didi

java - 重复和android服务,完成后自动调用

转载 作者:行者123 更新时间:2023-12-02 12:16:40 25 4
gpt4 key购买 nike

我不确定我是否有这个权利,但是我有一些代码需要在我的android应用程序的后台运行,一旦完成,我想等待10秒,然后运行再次输入代码。

我目前正在这样做,但我确信这不是正确的方法,并且想知道是否有人可以给我一个简单的例子,或者告诉我需要更改什么才能使其成为“正确的方法” “去做吧。

首先我有 ScheduleService.java 文件。这是我想要运行它的代码的地方,正如您所看到的,一旦完成,它就会使线程 hibernate 10 秒,然后再次调用自身,但这意味着代码永远不会真正完成(您应该如果出现错误,请查看堆栈跟踪的长度!)

ScheduleService.java

public class ScheduleService extends Service {
private static final String TAG = "ScheduleService";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
startJob();
}
});

t.start();

return Service.START_STICKY;
}

private void startJob(){
// all my code is here, and i do what i need

// job completed. Rest for 10 seconds before doing another one
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

//do job again
startJob();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy(){
super.onDestroy();
}
}

现在启动服务代码,当我的应用程序启动时(在我的 MainActivity.java 文件中),我有以下内容

// stop just encase its already started
context.stopService(new Intent(context, ScheduleService.class));

// start service
context.startService(new Intent(context, ScheduleService.class));

为了确保设备重新启动时启动服务,我还有 StartOnBootReciever.java 代码

public class StartOnBootReciever extends BroadcastReceiver {
private static final String TAG = "Autostart";

/**
* Listens for Android's BOOT_COMPLETED broadcast and then executes
* the onReceive() method.
*/
@Override
public void onReceive(Context context, Intent arg1) {
Log.d(TAG, "BOOT_COMPLETED broadcast received. Executing starter service.");

// upload in background
Intent intent = new Intent(context, ScheduleService.class);
context.startService(intent);

// This code will start the application once the device has been restarted
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

然后在我的 AndroidManifest.xml 文件中,我有以下内容

<receiver android:enabled="true" android:exported="true" android:name="StartOnBootReciever" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:enabled="true" android:exported="false" android:name="com.idamigo.ticketmachine.ScheduleService" />

这一切都有效,但我觉得代码是自调用的,并且“永无止境”

最佳答案

在您的Service中,您使用了Thread.Sleep,这是不必要的。因为Service会在后台持续运行,直到你停止它或者系统停止它。

示例:

public class ScheduleService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Timer timer = new Timer();
timer.schedule(task,1,10000);
return Service.START_STICKY;
}

TimerTask task= new TimerTask() {
@Override
public void run() {
//do your task here.
}
};

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

仅当您需要在后台连续运行任务时才寻求服务。否则,使用 IntentService 。当 IntentService 没有工作时,它会被停止,所以你不需要自己管理它的状态。

关于java - 重复和android服务,完成后自动调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46118600/

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