How to call a method in a service from another activity

I have a problem calling my method in a service, my getambil_jmlgangguan() method.

Detailed code is here:

 public class GetCountDataGangguanService extends Service { public String JUMLAH_GANGGUAN =""; public static final String TAG = "MyServiceTag1"; GlobalKoneksi konek_url = new GlobalKoneksi(); GetJmlGangguanFunction jmlGangguanFUnctions = new GetJmlGangguanFunction(); private static String KEY_SUCCESS = "success"; private static String KEY_JUMLAH_DATA = "jumlah"; JSONArray jml_data_json = null; // UserFunctions userFunctions; UserFunctions userFunctions = new UserFunctions(); SessionManager session; @Override public void onCreate() { // TODO Auto-generated method stub mHandlers = new ArrayList<Handler>(); Toast.makeText(this, "GetCountDataGangguan.onCreate()", Toast.LENGTH_SHORT).show(); //getambil_jmlgangguan(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "GetCountDataGangguan.onBind()", Toast.LENGTH_SHORT) .show(); return messenger.getBinder(); } public class LocalBinder extends Binder { public GetCountDataGangguanService getServerInstance() { return GetCountDataGangguanService.this; } } @Override public void onDestroy() { // TODO Auto-generated method stub Log.d("GetCountDataGangguan.onDestroy()", "Sudah di destroy"); Toast.makeText(this, "GetCountDataGangguan.onDestroy()", Toast.LENGTH_SHORT).show(); super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this, "GetCountDataGangguan.onUnbind()", Toast.LENGTH_SHORT).show(); return super.onUnbind(intent); } public String getambil_jmlgangguan(){ session = new SessionManager(getApplicationContext()); session.checkLogin(); HashMap<String, String> user = session.getUserDetails(); String unit_id = user.get(SessionManager.KEY_UNITID); String regu_id = user.get(SessionManager.KEY_REGUID); JSONObject jsondatagangguan = jmlGangguanFUnctions.getcountdata( regu_id, unit_id); try { Log.d("JUMLAH_GANGGUAN",jsondatagangguan.getString(KEY_JUMLAH_DATA)); JUMLAH_GANGGUAN = jsondatagangguan.getString(KEY_JUMLAH_DATA); } catch (JSONException e) { e.printStackTrace(); } return JUMLAH_GANGGUAN; } } 

How can I call the getambil_jmlgangguan() method in another action, for example, MainActivity and periodically get the result of JUMLAH_GANGGUAN .

+4
source share
1 answer

I suggest you take a look at the following link, which has an example of how to bind to a service: http://developer.android.com/guide/components/bound-services.html

+1
source

Source: https://habr.com/ru/post/1484358/


All Articles