gpt4 book ai didi

Android:在服务中启动一个线程

转载 作者:行者123 更新时间:2023-11-29 20:57:15 25 4
gpt4 key购买 nike

我想在我自己的服务中启动一个从 Activity 启动的新线程。在这个线程中,我想每 3 秒更新一次数据库中的数据。我创建了数据库并在我的 onStartCommand() 方法中对其进行了初始化。我应该在哪里实现我的线程以及如何实现?

我试过了,但没有用,不幸的是,该应用程序将被关闭。如果不调用此方法,一切正常。

我创建了这个方法,我在我的 onStartCommand 中调用了它

private void startThreadUpdatingDatabase(){
Log.d("Database", "startThreadUpdatingDatabase(was called)");
new Thread(new Runnable() {
public void run(){
//do stuff
}
}).start();
}

最佳答案

如果你想开始一个循环任务,你可以尝试不同的方法:

1)报警
2)处理程序
3) TimerTask(我最不喜欢的)

报警:

private AlarmManager mAlarmManager;
private static final long ALARM_INTERVAL = 3 * 60 * 1000;

private void issueAlarm() {
if(mAlarmManager == null)
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance(Locale.US);
calendar.add(Calendar.MILLISECOND, (int) ALARM_INTERVAL);
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, intent, 0);
mAlarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), ALARM_INTERVAL, alarmIntent);
}

创建您的 AlarmReceiver:

public class AlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//Do DB Stuff here
}

}

并且不要忘记在 list 中注册它:

<receiver
android:name=".AlarmBroadcastReceiver"
android:exported="false" />

处理程序:

@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();

// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}

然后排队你发布的任务

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//As danny117 pointed out, multiple clients starting the service
//Can trigger this.
mServiceHandler.removeCallbacks(yourRunnable);
mServiceHandler.post(yourRunnable);
return super.onStartCommand(intent, flags, startId);
}

可运行的应该是这样的:

private Runnable yourRunnable = new Runnable() {
@Override
public void run(){
//DB work here
if(mServiceHandler != null)
mServiceHandler.postDelayed(this, ALARM_INTERVAL);
}
}

在服务停止后也进行清理:

@Override
public void onDestroy() {
super.onDestroy();
mServiceHandler.removeCallbacks(yourRunnable);
mServiceLooper.quit();
}

计时器:

创建你的计时器:

private Timer myTimer = new Timer();

创建循环计时器任务:

private void scheduleTask() {
myTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//Do DB stuff here
}
}, 0, ALARM_INTERVAL);
}

引用资料:
Scheduling Repeating Alarms
Creating a Service

关于Android:在服务中启动一个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27260511/

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