两分钟后我需要调用一个方法。我试过处理程序和计时器,但当应用程序关闭时,它就不工作了。然后我有呼叫计时器在服务和启动服务按钮点击和停止服务后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();
}

从我启动服务的主活动类。
stopService(new Intent(getContext(), MyService.class));
startService(new Intent(getContext(), MyService.class));

显示
<service android:name="MyService" android:enabled="true" android:exported="true"/>

注意:我只想按一下服务键,两分钟后停止服务。
请寻求帮助来解决此问题,或者如果除了服务之外还有其他好的解决方案。
更新:如果网络响应不成功,我如何处理重试。
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();
        }
    }
}
}

最佳答案

应用程序关闭后,您的服务将无法运行,请阅读background restriction
这并不是一个真正的Service用例。
你的问题的解决办法是AlarmManager。2分钟后设置一个确切的警报,当它触发时,您的接收器将被调用。在使用AlarmManger之前,请先阅读setExactAndAllowWhileIdle()setExact()
或者,您也可以使用WorkManager来设置精确的作业。但是,如果您不需要Job Constraints,这将是一个过度杀戮。因此,如果您的工作不依赖于任何约束,如网络连接或其他,您可以使用AlarmManger
注意:如果您的任务依赖于网络连接,那么您应该使用WorkManager。阅读Schedule tasks with WorkManager

10-07 20:02
查看更多