Ich bin neu in Android-Programmierung, ich möchte Service machen kann Daten vom Server bekommen, und wenn die Daten aktualisiert werden. Meine Anwendung zeigt die Benachrichtigung an. Aber meine Anwendung kann nur Benachrichtigungen anzeigen, wenn es geöffnet ist, der Dienst läuft, ich denke, die Dienste tun nichts. Danke für Ihre Hilfe.Wie kann ich Service bekommen kann Antwort vom Server mit okhttp bekommen?
Das ist mein Servicecode:
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");
}
}