gpt4 book ai didi

android - 持续在后台运行服务

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:50 24 4
gpt4 key购买 nike

持续在后台运行服务。例如,必须启动一项服务,即使应用已关闭,该服务也会显示一次 toast 消息 20 秒。

public class AppService extends IntentService {

public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

public AppService() {
super("AppService");
}

@Override
protected void onHandleIntent(Intent workIntent) {
Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_SHORT).show();
SystemClock.sleep(20000);
}
}

最佳答案

下面的代码适合我...

public class AppService extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}

关于android - 持续在后台运行服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43557840/

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