Denny Kurniawan
Denny Kurniawan

Reputation: 168

How i can make service can get response from server with okhttp?

i am new in Android Programming, i want to make service can get data from server, and if the data is updated. my application will show the notification. But, my application can show notification only when its first opened, the service is running, i think the services do nothing. Thanks for your help.

This is my Service code :

public class ServiceBaru extends Service {
    SharedPreferences sharedPreferences3;
    SharedPreferences.Editor editor3;
    SharedPreferences sharedPreferences2;
    SharedPreferences.Editor editor2;
    Timer time = new Timer();
    String url_baru, url_default, url_pakai;
    int idl_modif, ids_modif;
    static String idl = "";
    static String ids = "";
    Context mContext;
    public ServiceBaru() {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Handler handler = new Handler();
        sharedPreferences2 = getSharedPreferences("semua_url", MODE_PRIVATE);
        url_baru = sharedPreferences2.getString("url_masuk", "");
        sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
        editor3 = sharedPreferences3.edit();
        url_default = "192.168.1.215";
        if(url_baru.equals("")){
            url_pakai = url_default;
        }
        else {
            url_pakai = url_baru;
        }
    handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                //performe the deskred task
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("http://" + url_pakai + "/www/index.php/ambilid").build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //Sengaja Kosong
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //idl Menunjukkan Id Baru
                        // ids menunjukkan id lama dari shared preferences
                        sharedPreferences3 = getSharedPreferences("id_keluar", MODE_PRIVATE);
                        editor3 = sharedPreferences3.edit();
                        idl = response.body().string();
                        idl_modif = Integer.parseInt(idl);
                        ids_modif = sharedPreferences3.getInt("idpertama", 0);
                        if(ids_modif==0) {
                            ids = idl;
                            ids_modif = Integer.parseInt(ids);
                            editor3.putInt("idpertama", ids_modif);
                            editor3.commit();
                        }
                        else{
                            if(idl_modif>ids_modif){
                                NotificationCompat.Builder notif = new NotificationCompat.Builder(getApplicationContext()).setSmallIcon(R.drawable.logoutragunung).setContentTitle("UTRAPOS Mobile").setContentText("Ada Data Baru Yang Perlu Approve");
                                Intent notifklik = new Intent(getApplicationContext(), MainActivity.class);
                                PendingIntent contentnotif = PendingIntent.getActivity(getApplicationContext(), 0, notifklik, PendingIntent.FLAG_UPDATE_CURRENT);
                                notif.setContentIntent(contentnotif);
                                NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                                notificationManager.notify(1, notif.build());
                                editor3.putInt("idpertama", idl_modif);
                                editor3.commit();
                                System.out.println(idl_modif + ids_modif);
                            }
                        }
                    }
                });
            }
        }, 1000);
        System.out.println("Hallo Dari Services");
        return START_STICKY;
    }



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

    @Override
    public IBinder onBind(Intent intent) {

        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

}

Upvotes: 0

Views: 467

Answers (1)

Sujith Niraikulathan
Sujith Niraikulathan

Reputation: 2141

handler.postDelayed()

This will run your code for one time only. Even though you looped it by calling the same function after 1000 millis it wont work. You have to use JobSheduler to trigger your task periodically.

Upvotes: 1

Related Questions