2017-02-11 75 views
0

我是Android编程新手,我想让服务可以从服务器获取数据,并且数据是否更新。我的应用程序将显示通知。 但是,我的应用程序只能在第一次打开时显示通知,服务正在运行,我认为服务什么也不做。谢谢你的帮助。如何使服务可以通过okhttp从服务器获得响应?

这是我的服务代码:

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"); 
    } 

} 

回答

1
handler.postDelayed() 

这将运行你的代码只有一次。即使你在1000毫秒之后通过调用相同的函数来打开它,它也不会工作。您必须使用JobSheduler定期触发您的任务。