我需要在2分钟后打电话给一个方法。我尝试过处理程序和计时器,但是当应用程序关闭时,它就不能工作了。然后我在服务中调用计时器,并在2分钟后按一下按钮启动服务,然后停止服务。它可以工作,但问题是有时服务调用本身。下面是我的密码。
MyService类
public class MyService extends Service {
private static Retrofit retrofit = null;
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
Log.d("message", "start");
}
public void onFinish() {
Log.d("message", "finish");
callMyMethod();
stopService(new Intent(MyService.this, MainActivity.class));
}
}.start();
}MainActivity类,从我开始服务的地方开始。
stopService(new Intent(getContext(), MyService.class));
startService(new Intent(getContext(), MyService.class));清单
<service android:name="MyService" android:enabled="true" android:exported="true"/>注意:我只想在2分钟后按下按钮并停止服务。请帮助解决这个问题,或如果有其他好的解决方案以外的服务。
更新:如果网络响应不成功,我如何处理重试.
MyService类
public class MyService extends Service {
private static Retrofit retrofit = null;
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
Log.d("mess", "start");
}
public void onFinish() {
Log.d("mess", "finish");
selectDriverForJOb();
stopSelf();//(new Intent(MyService.this, MainActivity.class));
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
private void selectDriverForJOb() {
SharedPreferences sharedPreferences = getSharedPreferences("Login_credentials", MODE_PRIVATE);
String user_id = sharedPreferences.getString("userID", null);
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
APIServices_interface selectDriver_interface = retrofit.create(APIServices_interface.class);
Call<SelectDriverforJobResult> call = null;
try {
call = selectDriver_interface.selectDriverforJob(user_id);
} catch (Exception e) {
e.printStackTrace();
}
if (call != null) {
try {
call.enqueue(new Callback<SelectDriverforJobResult>() {
@Override
public void onResponse(Call<SelectDriverforJobResult> call, Response<SelectDriverforJobResult> response) {
SelectDriverforJobResult selectDriver = response.body();
String message = selectDriver.getMessage();
if(!(message.equalsIgnoreCase("success"))){
}
}
@Override
public void onFailure(Call<SelectDriverforJobResult> call, Throwable t) {
Log.d("mess : ", "Error Updating ");
//want to retry
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}发布于 2018-06-27 06:28:25
覆盖服务中的onStartCommand并返回START_NOT_STICKY --它不会重新启动
public void onStartCommand()
{
return START_NOT_STICKY
}使用stopSelf()在stopService(new Intent(getContext(), MyService.class));中的应用
发布于 2018-06-27 06:20:43
您的服务不会在应用程序关闭后运行,请阅读背景限制。这实际上不是一个Service用例。
解决您的问题的方法可以是AlarmManager。在2分钟后设置一个精确的警报,当它被触发时,你的接收器会被呼叫。在使用AlarmManger之前,先读读setExactAndAllowWhileIdle()和setExact()。
或者,您也可以使用WorkManager来设置确切的作业。但是,如果您不需要Job Constraints,这将是过度的。因此,如果您的工作不依赖于任何约束,如网络连接或其他,您可以使用AlarmManger。
PS:-如果您的任务依赖于网络连接,那么您应该使用WorkManager。读用WorkManager调度任务。
https://stackoverflow.com/questions/51054739
复制相似问题