gpt4 book ai didi

android - onStartCommand 在服务 android 中只调用一次?

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:55 25 4
gpt4 key购买 nike

我有一个简单的服务需要在后台运行。下面是该服务的代码。我想重复运行 onStartCommand 中的代码只是为了测试目的我显示了 toast。但是 Toast 也只调用一次

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;




/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions and extra parameters.
*/
public class WiFiCheck extends Service {




@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.


Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();


return super.onStartCommand(intent, flags, startId);

}

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



/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}

/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}

但是 onStartCommand 只调用一次,只调用一次。

我用过

return START_STICKY;

启动服务如下

        startService(new Intent(this, WiFiCheck.class));

但还是没用。求助

最佳答案

如果重新启动服务,Toast 将被调用两次或更多次

例如

startService(new Intent(this, WiFiCheck.class));
startService(new Intent(this, WiFiCheck.class));

new IntentonStartCommand(Intent intent

关于android - onStartCommand 在服务 android 中只调用一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922332/

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