Невозможно получить данные с http-сервера с помощью Foreground Service - PullRequest
0 голосов
/ 31 марта 2020

На самом деле, мне нужно постоянно читать данные с сервера, даже если мое приложение удалено из-за того, что я использовал «FOREGROUND_SERVICE». Но у меня это не работает нормально, у меня есть какой-нибудь подходящий способ получить мой Требование ??? Код моего сервиса '' 'publi c класс MyService расширяет сервис {private Intent serviceIntent;

private int randnum=0;
private boolean gen;
class MyserviceBinder extends Binder
{
    public MyService getService(){
        return MyService.this;
    }
}
private IBinder mBinder =new MyserviceBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //getdata();
    System.out.println("hai");
    Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        Notification notification = new NotificationCompat.Builder(MyService.this,Channel_Id)
                .setContentTitle("Status")
                .setContentText("Water saver is running background services..")
                .setSmallIcon(R.drawable.ic_android)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build();
        System.out.println("heloo");
        startForeground(1,notification);
    }


    return START_NOT_STICKY;
}




public void getdata() {
    System.out.println("In GetData service");
    OkHttpClient client=new OkHttpClient();
    String ul="http://192.168.29.58:8080/";
    Request request=new Request.Builder().url(ul).build();


        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("Failed to get data   " + e.getMessage());

                new Thread(new Runnable() {
                    @Override
                    public void run() {


                        Toast.makeText(getApplicationContext(), "Turn on wif and retry again...", Toast.LENGTH_LONG).show();
                        System.out.println("Failed To connect");

                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response)throws IOException {

                if (response.isSuccessful()) {
                    final String s = response.body().string();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("Data read from Service: " + s);
                            int a = Integer.parseInt(s);
                            sendNotification(s);
                        }
                    });
                }
            }
        });

    }

' ''

...